GridView中实现单选RadioButton
?
注意:若在gridview內(nèi)對radiobutton用組名groupname同名方法行不同,若加上GroupName="skytest"
查看靜態(tài)頁面源代碼便知道:兩個name值的組名都不同。
?
下面結(jié)合js實現(xiàn)單選.
呈現(xiàn)頁:
?<asp:GridView ID="GridView1" Width="960px" EmptyDataText="暫無標(biāo)書可操作"
????????????DataKeyNames="ID" runat="server"?onrowdatabound="GridView1_RowDataBound"
????????????>
????????????<Columns>
????????????????<asp:TemplateField HeaderText="選擇">
????????????????????<ItemTemplate>
????????????????????????<asp:RadioButton ID="RadioButton1" runat="server"????/>
????????????????????</ItemTemplate>
????????????????</asp:TemplateField>
????????????????<asp:BoundField DataField="ID" HeaderText="標(biāo)書編號" />
??????????</Columns>
??</asp:GridView>
代碼頁:
?protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
????????{
????????????if (e.Row.RowType == DataControlRowType.DataRow)
????????????{
????????????????RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
????????????????if (rb != null)
????????????????{
????????????????????rb.Attributes.Add("onclick", "single(this)");??//single(obj)為js函數(shù)
????????????????}
????????????}
????????}
需要添加的javascript函數(shù):
<script type="text/javascript">
????var last=null;
????function single(obj)
????{
??????if(last==null)????//第一次選擇RadioButton時賦id值給last
??????{
?????????last=obj.id;?
??????}
??????else????????????//第一次以后的每一次都在這運行,把上此的RadioButton.Checked=false,記下此次的obj.name
??????{
????????var lo=document.getElementByIdx_x(last);
????????lo.checked=false;
????????last=obj.name;
??????}
??????obj.checked="checked";???//添加checked屬性,以便在上邊賦值為false
????}
????</script>
總結(jié)
以上是生活随笔為你收集整理的GridView中实现单选RadioButton的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给GridView中的buttonFie
- 下一篇: GridView中的RadioButto