Radiobutton on Repeater?

1

I need to do an OnCheckedChanged on a RadioButton inside a repeater, but I could not do it the normal way. Is there a way to create an event on a radioButton inside a Repeater?

<asp:Repeater ID="rptDiasSemana" runat="server" OnItemDataBound="rptDiasSemana_ItemDataBound" OnItemCommand="rptDiasSemana_ItemCommand">
    <ItemTemplate>
        <div style="border-top: 1px solid none;">

            <asp:CheckBox runat="server" ID="chkDiaSemana" CssClass="checkbox" />
        </div>
        <div class="row form-group col-md-12">
            <asp:RadioButton runat="server" ID="rbtnDiaTodo" Text="<%$ Resources:UIResource, DiaTodo %>" GroupName="autoTicketHora" CssClass="radio" />
        </div>
        <div class="row form-group">
            <div class="col-md-2">
                <asp:RadioButton runat="server" ID="rbtnParcial" Text="<%$ Resources:UIResource, Parcial %>" GroupName="autoTicketHora" CssClass="radio" OnCheckedChanged="rbtnParcial_CheckedChanged" ClientIDMode="Static" AutoPostBack="true" />
            </div>
            <div id="divData" runat="server" visible="false">
                <span class="col-xs-1 text-right"><%= Resources.UIResource.DePontos.Replace(":", "") %></span>
                <div class="col-md-1">

                </div>
            </div>
        </div>
        <div class="row form-group">
        </div>
    </ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{       

    if (!IsPostBack)
    {

            lstAutoTicketHorario = new List<AutoTicketHorario>()
        {
            new AutoTicketHorario(){DiaSemana = '1', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '2', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '3', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '4', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '5', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '6', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true},
            new AutoTicketHorario(){DiaSemana = '7', HorarioInicio = "00:00", HorarioFim = "00:00", Escolhido = true}
        };
    }

    rptDiasSemana.DataSource = lstAutoTicketHorario;
    rptDiasSemana.DataBind();

}

protected void rbtnParcial_CheckedChanged(object sender, EventArgs e)
{
    foreach (RepeaterItem item in rptDiasSemana.Items)
    {
        RadioButton rbtnParcial = (RadioButton)item.FindControl("rbtnParcial");
        if (rbtnParcial.Text.Equals((sender as RadioButton).Text))
        {
            CommandEventArgs commandArgs = new CommandEventArgs("SomeCommand", rbtnParcial.Text);
            RepeaterCommandEventArgs repeaterArgs = new RepeaterCommandEventArgs(item, rbtnParcial, commandArgs);
            rptDiasSemana_ItemCommand(rbtnParcial, repeaterArgs);
        }
    }
}

protected void rptDiasSemana_ItemCommand(object source, RepeaterCommandEventArgs e)
{            
    RadioButton rbtnParcial = (RadioButton)e.Item.FindControl("rbtnParcial");
    if (rbtnParcial.Checked)
    {
        e.Item.FindControl("divData").Visible = true;
    }
}
    
asked by anonymous 21.03.2018 / 22:22

1 answer

0

What happens is that with each Postback that your RadioButton executes, the Repeater Databind occurs again, causing the source element and event to be lost. Try moving the two lines:

rptDiasSemana.DataSource = lstAutoTicketHorario;
rptDiasSemana.DataBind();

Inside the block:

if (!IsPostBack)
{

}
    
27.03.2018 / 16:31