大家好我用网格视图将多行记录插入数据库。这就是我的网格视图看起来像
的方式问题是可以选择所有单选按钮,我只需要为每一行选择1个单选按钮。我的网页表单的工作方式是用户必须使用单选按钮从两个文本框中选择正确的答案,然后我必须将检查的答案提交给数据库。这有可能吗?
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;
}
答案 0 :(得分:8)
我假设你在谈论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时,无法选择橙色。如果在选择Apple时选择了橙色,Apple将自动取消选中。但是当选择Apple时,当您单击Blue时,Apple将不会被取消选中。
答案 1 :(得分:1)
尝试使用像
这样的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
希望它有效。
答案 2 :(得分:1)
<input id="HDDepMood_0" name="DepMood" type="radio" runat="server" />
Same name for the one group.
答案 3 :(得分:0)
我想你可以这样做: -
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;
}
答案 4 :(得分:0)
只需在页面中添加一些脚本即可设置组名:
$('input[type=radio]').each(function () {
var array = $(this).attr('name').split('$');
$(this).attr('name', array[array.length - 1]);
});
这将调整呈现为正确组名的名称。
答案 5 :(得分:0)
.gitignore