Radiobutton on the Repeater?

Asked

Viewed 72 times

1

I need to make an Oncheckedchanged on a Radiobutton inside a Repeater, but I could not do it the normal way. there is how 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;
    }
}
  • What didn’t work out?

  • It does not enter the rbtnParcial_CheckedChanged event, nor the breakpoint event.

  • is a punctual problem, it is not easy to reproduce, but as Repeater has also events can be this... !!! it is hard to know without seeing

1 answer

0

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

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

Into the block:

if (!IsPostBack)
{

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.