GridView中的RadioButton列之间不能互斥
 GridView中的RadioButton列與CheckBox列
 GridView擁有大量的內(nèi)置功能,可以使用不同的默認(rèn)filed來(lái)放置顯示諸如TextBox、Buttos等等控件,支持模板是GridView的最大的功能,可以添加額外的、GridView沒(méi)有的功能,例如RadioButton列。
 RadioButton可以讓用戶只選中一列,而CheckBox可以選中多列。
 可能首先想到的GridView中不包含有RadioBox列,是在ItemTemplate中添加RadioBox列,好像不行,這些RadioBox不會(huì)相互排斥,最終是用戶可以選中多列。
 代碼段1:
 <asp:GridView ID="RadioCheck" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
 ???? <Columns>
 ???????? <asp:TemplateField HeaderText=".">?
 ???????????? <ItemTemplate>
 ???????????????? <asp:RadioButton ID="btnRadio" runat="server" GroupName="ProductGroup" />
 ???????????? </ItemTemplate>
 ???????? </asp:TemplateField>
 ???????? <asp:BoundField DataField="ProductName" HeaderText="ProductName" />
 ???????? <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
 ???? </Columns>
 </asp:GridView>
 
 
 設(shè)置了GroupName:作用在于讓RadioButton在一個(gè)組里,產(chǎn)生互斥的效果,只能選中一個(gè),但這樣的效果卻是能選中多個(gè)值,如圖Radio1.jpg
 
 也就是說(shuō)RadioButton沒(méi)有起到要的作用,沒(méi)有互斥性,查看源代碼,可以發(fā)現(xiàn)問(wèn)題:
 <input id="RadioCheck_ctl02_btnRadio" type="radio" name="RadioCheck$ctl02$ProductGroup" value="btnRadio" />
 <input id="RadioCheck_ctl03_btnRadio" type="radio" name="RadioCheck$ctl03$ProductGroup" value="btnRadio" />
 ........
 <input id="RadioCheck_ctl10_btnRadio" type="radio" name="RadioCheck$ctl10$ProductGroup" value="btnRadio" />,上面的RadioButton根本沒(méi)有在一個(gè)組里面,GroupName也起不了作用。
 解決方法:
 ??1.移除RadioButton控件,換成Literal控件,ID為RadioButtonMarkup
 ??2.為GridView的RowCreated事件創(chuàng)建事件,RowCreated事件如下:只要在GridView中新增一行數(shù)據(jù),就觸發(fā)RowCreated事件,不用RowDataBound事件的原因是:只有當(dāng)數(shù)據(jù)明確綁定到控件中才引發(fā)RowDataBound事件。處理如下:在每一行記錄中,編程引用Literal控件,然后在其Text屬性里面聲明代碼,創(chuàng)建RadioButton,name值為ProductGroup,id為RowSelectX,X為index值,value值也為index值。
?
 protected void Product_RowCreated(object sender, GridViewRowEventArgs e)
 {
 ??if(e.Row.RowType == DataControlRowType.DataRow)
 ??{
 ?? Literal output = e.Row.FindControl("RadioButtonMarkup") as Literal;
 ?? output.Text = string.Format(@"<input type="radio" name="ProductGroup"" " + @"id="RowSelector{0}"value="""value{0}",e.Row.RowIndex);
 ??}
 }
 
 
 當(dāng)然這是后臺(tái)使用的方法.
 還有一種就是用Javascript腳本事件
 .aspx?
 
 ????<script type="text/javascript">
 ????var last=null;
 ????function judge(obj)
 ????{
 ??????if(last==null)
 ??????{
 ???????? last=obj.id;
 ??????}
 ??????else
 ??????{
 ????????var lo=document.getElementById(last);
 ????????lo.checked=false;
 ????????last=obj.name;
 ??????}
 ??????obj.checked="checked";
 ????}
 ????</script>???
 
 .cs 頁(yè)面里在gridview的 RowDataBound 事件里
 ?? protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 ????{
 ????????if (e.Row.RowType == DataControlRowType.DataRow)
 ????????{
 ????????????RadioButton rb = (RadioButton)e.Row.FindControl("rdoChoose");
 ????????????if (rb != null)
 ????????????{
 ????????????????rb.Attributes.Add("onclick", "judge(this)");
 ????????????}
 ????????}
 ????}
 
 注意:如果你有RadioButton同時(shí)還有別的JS事件,請(qǐng)一定要追加上,否則就不能正確顯示了.我比較偏愛(ài)JS這種方法,速度快,而且試過(guò)了放在UpdatePannel里也可以.沒(méi)有過(guò)多的復(fù)雜測(cè)試.
總結(jié)
以上是生活随笔為你收集整理的GridView中的RadioButton列之间不能互斥的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: GridView中实现单选RadioBu
- 下一篇: 给gridview动态生成radiobu
