【问题标题】:Allowing only one radio button to be checked in each row of grid view只允许在网格视图的每一行中选中一个单选按钮
【发布时间】:2013-05-30 06:35:07
【问题描述】:

大家好,我使用网格视图将多行记录插入数据库。这就是我的网格视图的样子

问题是可以选择所有单选按钮,我只需要为每一行选择一个单选按钮......我的 web 表单的工作方式是用户必须使用单选按钮从两个文本框中选择正确的答案,然后我必须将选中的答案提交到数据库。这可能吗?

aspx:`

             <asp:ButtonField Text="SingleClick" CommandName="SingleClick"
                    Visible="False" />

                    <asp:TemplateField HeaderText="Question">
                        <ItemTemplate>
                            <br />
                            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Answer">
                        <ItemTemplate>
                            <br />
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            <asp:RadioButton ID="RadioButton1" runat="server" />
                            <br />
                            <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                            <asp:RadioButton ID="RadioButton2" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <FooterTemplate>
                            <asp:Button ID="btnAdd" runat="server" Text="+" onclick="btnAdd_Click1" />
                        </FooterTemplate>

                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    </td>
</tr>`

后面的代码:

  protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        //Clear the existing selected row 
        foreach (GridViewRow oldrow in GridView1.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;

    }

【问题讨论】:

  • 粘贴您的网格标记和相关代码
  • 我是新手,我没有写太多代码,因为我完全不知道......更新了
  • 你为什么不用radiobuttonlist
  • 您也可以使用 GroupName 属性。
  • GroupName 属性有帮助吗?

标签: c# gridview


【解决方案1】:

我假设您说的是 ASP.NET。您是否尝试将单选按钮组合在一起?

这是一个例子。

<asp:RadioButton id="rBtn1" GroupName="Fruits"
         Text="Apple" runat="server"/>
<asp:RadioButton id="rBtn2" GroupName="Fruits"
         Text="Orange" runat="server"/>
<asp:RadioButton id="rBtn3" GroupName="Colors"
         Text="Blue" runat="server"/>
<asp:RadioButton id="rBtn4" GroupName="Colors"
         Text="Red" runat="server"/>

选择 Apple 时,无法选择 Orange。如果在选择 Apple 的同时选择了 Orange,Apple 将自动取消选择。但是当Apple被选中时,当你点击Blue时,Apple不会被取消选中。

【讨论】:

    【解决方案2】:

    尝试使用 javascript 之类的函数

    <script language="javascript" type="text/javascript">
    function SelectSingleRadiobutton(rdbtnid) {
    var rdBtn = document.getElementById(rdbtnid);
    var rdBtnList = document.getElementsByTagName("input");
    for (i = 0; i < rdBtnList.length; i++) {
    if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
    {
    rdBtnList[i].checked = false;
    }
    }
    }
    </script>
    

    网格视图

    <asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" DataKeyNames="UserId" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="UserName" HeaderText="Name"/>
    <asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="LastName" />
    <asp:BoundField DataField="Location" HeaderText="Location" />
    </Columns>
    </asp:GridView>
    

    尝试检查aspdotnet

    查看asp.net

    希望它有效。

    【讨论】:

    • 这将每行生成一个单选按钮..但是OP希望每行有两个单选按钮..他不担心多行单选按钮选择......他关心的是一行内的多个不应选择单选按钮..
    【解决方案3】:
    <input id="HDDepMood_0" name="DepMood" type="radio"  runat="server" />
    Same name for the one group. 
    

    【讨论】:

      【解决方案4】:

      我认为你可以这样进行:-

      protected void rbtnSelect_CheckedChanged(object sender, EventArgs e)
      {
      RadioButton selectButton = (RadioButton)sender;
      GridViewRow gvrow = (GridViewRow)selectButton.Parent.Parent;
      var radio = (RadioButton)gvRow.FindControl("rdo");// rdo is ID of your radio button:-
      if(radio[0].id=selectButton.id)
      radio[1].checked=false;
      else if(radio[1].id=selectButton.id)
      radio[0].checked=false;
      }
      

      【讨论】:

      • 但是我有 2 个具有 2 个不同 ID 的单选按钮,我们可以找到组名的控制吗?还有 .id 是从哪里来的。感谢您的帮助
      【解决方案5】:

      只需在页面中添加一些脚本来设置组名:

      $('input[type=radio]').each(function () {
          var array = $(this).attr('name').split('$');
          $(this).attr('name', array[array.length - 1]);
      });
      

      这会将呈现的名称调整为正确的组名。

      【讨论】:

        【解决方案6】:
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script language="javascript">
            function singleRbtnSelect(chb) {
                $(chb).closest("table").find("input:radio").prop("checked", false);
                $(chb).prop("checked", true);
            }
        </script>  
        

        【讨论】:

        • 单选按钮的onclick事件 - 在单选按钮的gridview中调用此函数
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-04
        • 1970-01-01
        • 2018-08-01
        • 1970-01-01
        • 2010-11-02
        • 1970-01-01
        • 2021-03-04
        相关资源
        最近更新 更多