protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
int row = ((GridViewRow)((RadioButton)sender).NamingContainer).RowIndex; //获得选中行
foreach (GridViewRow allrow in this.GridView1.Rows)
{
if (allrow.RowIndex != row)
{
RadioButton rb = (RadioButton)allrow.FindControl("RadioButton1");
//把其他行的radiobutton.checked=false
rb.Checked = false;
}
}
}
aspx页面
<asp:TemplateField>
<HeaderTemplate>
<asp:label runat="server" Text="更改"></asp:label>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton runat="server" oncheckedchanged="RadioButton1_CheckedChanged" ID="RadioButton1"
AutoPostBack="true" ></asp:RadioButton> //!!!!!!!!!!!!!!!!!!(AutoPostBack="true")
非常重要的一步
</ItemTemplate>
</asp:TemplateField>

本文介绍如何在ASP.NET的GridView控件中实现单选按钮组的联动效果,即当点击一行中的单选按钮时,其他行的相同按钮会自动取消选择,通过设置AutoPostBack属性并使用事件处理来实现。
5782

被折叠的 条评论
为什么被折叠?



