C# 只允许在网格视图的每一行中选中一个单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16829490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:53:36  来源:igfitidea点击:

Allowing only one radio button to be checked in each row of grid view

c#gridview

提问by user2376998

Hi guys i used a grid view to insert multiple rows of record into the database . this is how my grid view looks like enter image description here

大家好,我使用网格视图将多行记录插入到数据库中。这就是我的网格视图的样子在此处输入图片说明

The problem is that all radio buttons can be selected , i need only 1 radio button to be selected for each row .. . My web form works in the way where user have to select the correct answer out of the two textboxes with a radio button then i will have to submit the checked answer to database . is this possible?

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

aspx : `

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

code behind :

后面的代码:

  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;

    }

回答by Jieqin

I assume you're talking about ASP.NET. Did you try grouping the radio buttons together?

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

Here's an example.

这是一个例子。

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

When Apple is selected, Orange can't be selected. If Orange is selected while Apple is selected, Apple will automatically be un-selected. But when Apple is selected, when you click Blue, Apple won't be un-selected.

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

回答by Rahul

Just try to use a javascriptfunction like

尝试使用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>

Grid View

网格视图

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

Try to check aspdotnet

尝试检查aspdotnet

Check this asp.net

检查这个asp.net

Hope it works.

希望它有效。

回答by Pranav

I think you may proceed like this :-

我想你可以这样继续:-

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

回答by DaanVis

Just add some script to your page to set the groupname:

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

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

This wil adjust the name rendered to the correct group name.

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

回答by Alex Mathew

<input id="HDDepMood_0" name="DepMood" type="radio"  runat="server" />
Same name for the one group. 

回答by Bhhruguni

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