GridView绑定radiobutton以后实现唯一选择,互斥
今日回答某贴时随笔备忘如下
楼上诸位的方法在gridview中无法实现哟
我刚才帮楼主试了下
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="tester" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
是不能实现排斥的
因为radiobutton的排斥在html中实际上就是name值相同的radion会相互排斥
但是在gridview中的radionbutton的name值是会被默认修改为
<input id="GridView1_ctl02_RadioButton2" type="radio" name="GridView1$ctl02$tester" value="RadioButton2" />
name会被改变 = =#
这样可以解决
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow g = e.Row;
Label lab=(Label) g.Cells[0].FindControl("Label2");
if (lab != null)
{
lab.Text = " <input id=/""+lab.ClientID+"/" type=/"radio/" name=/"RadioButton1/" value=/"1/" />";
}
}