Gridview连续多个单选按钮

时间:2012-11-04 23:25:45

标签: asp.net gridview radio-button

Gridview连续多个radiobuttons

我在Gridview中连续有三个RadioButtons。我需要获得检查状态,但是当您选择按钮时它永远不会改变。 这是代码:

    <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" >

背后:

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();
    }

1 个答案:

答案 0 :(得分:1)

修改标记以在gridview

中包含OnRowCommand事件处理程序
<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>

这是OnRowCommand事件处理程序背后的代码

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;
           }
    }
}

请确保您仅在页面加载时绑定数据,而不是在页面回发上绑定。

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();
    }
}