【问题标题】:Gridview multiple radiobuttons in a rowGridview连续多个单选按钮
【发布时间】:2012-10-24 19:30:53
【问题描述】:

Gridview 一行中的多个单选按钮

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

【问题讨论】:

  • 发布命令按钮的 HTML 标记?

标签: asp.net gridview radio-button


【解决方案1】:

修改您的标记以在网格视图中包含 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();
    }
}

【讨论】:

  • GridView 标记中已经有 OnRowCommand。也尝试使用 radio!=null 并没有命中。
  • 请确保,您仅在 Page_Load 上绑定数据,我已更新答案以添加此内容。
  • 是的,我正在绑定 Page_Load。我还有其他关于这个 gridview 的事件,女巫也使用绑定(其他来源)。此收音机不使用数据源。
  • 在这种情况下,问题可能在于您在问题中未提及的其他一些数据绑定。我有这个工作在本地绑定到一个字符串列表,它工作正常。您可能会考虑添加完整的标记和代码。每次都有数据绑定,它会重置单选按钮状态。
  • 我想我不需要使用 if page.isPostBack。添加了更多代码。
猜你喜欢
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 2019-10-04
  • 2015-09-05
  • 1970-01-01
  • 2011-03-12
相关资源
最近更新 更多