esteban
esteban

Reputation: 63

Gridview multiple radiobuttons in a row

Gridview multiple radiobuttons in a row

I have three RadioButtons in a row in a Gridview. I need to get the checked status, but it never changes when you select a button. Here is the code:

    <asp:TemplateField>
         <ItemTemplate>
               <asp:RadioButton runat="server" GroupName="venc" ID="rdo0" Checked="True" />
         </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="2° Venc.">
         <ItemTemplate>
              <asp:RadioButton runat="server" GroupName="venc" ID="rdo3"    />
          </ItemTemplate>
       </asp:TemplateField>


    protected void gvCuotas_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        for (int i = 2; i < 5; i++)
        {
            radio = (RadioButton)fila.FindControl("rdo" + i);
            if (radio.Checked)
               {
                  vencimiento = i; //never gets here.
                  break;
               }
            }
        }
   }

ASP.

<asp:GridView ID="gvCuotas" runat="server" AutoGenerateColumns="False" 
                        Caption="Cuotas Pendientes"  capCellSpacing="2" CellPadding="2" 
                    HeaderStyle-BackColor="Aqua" HorizontalAlign="Center" Width="80%" 
                        BorderStyle="Solid" onrowcommand="gvCuotas_RowCommand" 
                        onrowdatabound="gvCuotas_RowDataBound" >

Behind:

public partial class Cuota1 : System.Web.UI.Page
{
    DataClasses1DataContext baseAlumnos = new DataClasses1DataContext();

    protected void Page_Load(object sender, EventArgs e)
    {

        var cuotas = from p in baseAlumnos.Cuotas where p.Pagado.Equals(0) 
                     select new { p.Mes, p.Observacion, p.Vencimientos, p.Pagado};


        gvCuotas.DataSource = cuotas;
        gvCuotas.DataBind();
    }

Upvotes: 0

Views: 3523

Answers (1)

Tariqulazam
Tariqulazam

Reputation: 4585

Modify your Markup to include the OnRowCommand event handler in the gridview

<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" OnRowCommand="OnRowCommand">
    <Columns>
        <asp:TemplateField>
         <ItemTemplate>
               <asp:RadioButton runat="server" GroupName="venc" ID="rdo0" Checked="True"/>
         </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="2° Venc.">
         <ItemTemplate>
              <asp:RadioButton runat="server" GroupName="venc" ID="rdo3"    />
          </ItemTemplate>
       </asp:TemplateField>
       <asp:ButtonField HeaderText="Confirm" ButtonType="Button" />
    </Columns>
</asp:GridView>

Here is the code behind for OnRowCommand event handler

protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{

    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow gvRow = gv.Rows[index]; 

    for (int i = 2; i < 5; i++)
    {
        var radio = (RadioButton)gvRow.FindControl("rdo" + i);
        if (radio!=null && radio.Checked)
           {
              //This point will hit for the selected radiobutton 
              break;
           }
    }
}

Please make sure, you are binding your data only on page load, not on page postback.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){ 
        var cuotas = from p in baseAlumnos.Cuotas where p.Pagado.Equals(0) 
                 select new { p.Mes, p.Observacion, p.Vencimientos, p.Pagado};
        gvCuotas.DataSource = cuotas;
        gvCuotas.DataBind();
    }
}

Upvotes: 1

Related Questions