【发布时间】: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 属性有帮助吗?