【问题标题】:Mutually exclusive selection of Radiobutton in gridView.ASP.NET C#gridView.ASP.NET C#中单选按钮的互斥选择
【发布时间】:2011-09-24 22:06:45
【问题描述】:
    <asp:TemplateField HeaderText="Select One">

    <ItemTemplate>
    <asp:RadioButton ID="RadioButton1" runat="server" />

    </ItemTemplate>

    </asp:TemplateField> 

aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow di in GridView1.Rows)
    {
        RadioButton rad = (RadioButton)di.FindControl("RadioButton1");

        if (rad.Checked&&rad!=null)
        {
            s = di.Cells[1].Text;
        }

    }

    Response.Redirect("applicants.aspx?form=" +s);

}

我正在选择用这个选择的行,但我在这里遇到问题我希望用户只能选择一个radiobutton,但它允许一次选择所有radiobuttons。你能请帮我解决这个问题。 请。

【问题讨论】:

    标签: c# asp.net gridview radio-button


    【解决方案1】:

    也许我在聚会上来得太晚了,但这样就可以了, check out here......

    这可能对将来观看此答案的人有用。

    【讨论】:

      【解决方案2】:

      在 ASP 页面中,您需要将所有单选按钮的 GroupName 属性设置为相同,例如:

      <asp:RadioButton ID="RadioButton1" runat="server" GroupName="RadioGroup" />
      

      【讨论】:

      • 我只有一个 ID RadioButton1,它将根据结果在每一行中动态创建。
      • GroupName 的值存储在视图状态中,是否可以禁用视图状态? msdn.microsoft.com/en-us/library/…
      【解决方案3】:

      为此,您可以使用以下代码 首先,您应该定义 groupName。以下代码将起作用

       <asp:RadioButton ID="RadioButton1" OnCheckedChanged="rbSelector_CheckedChanged" AutoPostBack="true" GroupName="Apply" runat="server"></asp:RadioButton>
      

      C#

      protected void rbSelector_CheckedChanged(object sender, System.EventArgs e)
      {
          foreach (GridViewRow oldrow in GridView2.Rows)
          {
              ((RadioButton)oldrow.FindControl("RadioButton1")).Checked = false;
          }
      
      
          //Set the new selected row
          RadioButton rb = (RadioButton)sender;
          GridViewRow row = (GridViewRow)rb.NamingContainer;
          ((RadioButton)row.FindControl("RadioButton1")).Checked = true;
      }
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:

          单独使用GroupName 属性将不起作用,每个单选按钮仍将获得唯一的名称属性,因为它们位于网格的不同行中。

          一种选择是使用文字控件 (example) 手动发出单选按钮标记。这将使在客户端对单选按钮进行分组变得容易,但需要更多的工作来确定在回发时选择了哪个按钮。

          当我需要这种行为时,我发现将单选按钮保留为服务器端控件更容易,并且只需使用 jQuery 强制执行按钮组。如您所示,将您的RadioButton 放入 TemplateField 中,然后添加此代码以在选中一个按钮时取消选中所有其他按钮:

          $(document).ready(function () {
              // could also pass in a unique ID selector
              createManualRadioButtonGroupForGridView(".myGridViewClass");
          });
          
          function createManualRadioButtonGroupForGridView(gridViewSelector) {
              $(gridViewSelector + " input[type=radio]").change(function () {
                  var checkedRadioButton = this;
                  $(gridViewSelector + " input[type=radio]").each(function (e) {
                      if (this !== checkedRadioButton) {
                          $(this).prop("checked", false);
                      }
                  });
              });
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-24
            • 1970-01-01
            • 1970-01-01
            • 2017-07-24
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多