您好,登錄后才能下訂單哦!
今天開發(fā)碰見一個(gè)問題,就是當(dāng)GridView中加入一個(gè)包含RadioButton的模板列,結(jié)果一運(yùn)行。。。。。天啊,單選按鈕可以多選了! 囧??!為了演示一下我今天的錯(cuò)誤我還是模擬一個(gè)功能場景吧,我要實(shí)現(xiàn)的功能是顯示一個(gè)包含單選按鈕的學(xué)生信息列表,選擇一行后將詳細(xì)信息顯示出來~!
1.問題展現(xiàn)
①首先準(zhǔn)備一個(gè)GridView用來展示學(xué)生的基本信息與最重要的單選按鈕,代碼如下:
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
- <Columns>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:RadioButton ID="rbStudent" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="SName" HeaderText="用戶名" />
- <asp:BoundField DataField="SSex" HeaderText="性別" />
- </Columns>
- </asp:GridView>
②接下來準(zhǔn)備需要綁定數(shù)據(jù)的實(shí)體,代碼如下:
- public class Student
- {
- public string SID { get; set; }
- public string SName { get; set; }
- public string SSex { get; set; }
- }
③初始化數(shù)據(jù),綁定GridView列表,代碼如下:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- this.BindGridView();
- }
- }
- public void BindGridView()
- {
- List<Student> sList = new List<Student>()
- {
- new Student(){ SID = "s001", SName="張三", SSex="男"},
- new Student(){ SID = "s002", SName="李四", SSex="女"},
- new Student(){ SID = "s003", SName="王五", SSex="男"}
- };
- GridView1.DataSource = sList;
- GridView1.DataBind();
- }
什么原因呢?細(xì)心的人可能會說RadioButton你沒有設(shè)置GroupName屬性所以不屬于一組,但事實(shí)我設(shè)置了該屬性后還是無效!
2.解決思路
① 問題分析
在運(yùn)行后,我右鍵查看源代碼后發(fā)現(xiàn)了這個(gè)詭異的問題,原來是GridView會自動(dòng)給input 的name屬性賦值,導(dǎo)致了每一行的name屬性都不一樣,造成了不能單選的問題,GridView生成代碼如下:
- <table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
- <tr>
- <th scope="col"> </th><th scope="col">用戶名</th><th scope="col">性別</th>
- </tr><tr>
- <td>
- <input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" />
- </td><td>張三</td><td>男</td>
- </tr><tr>
- <td>
- <input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" />
- </td><td>李四</td><td>女</td>
- </tr><tr>
- <td>
- <input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" />
- </td><td>王五</td><td>男</td>
- </tr>
- </table>
我第一個(gè)想到的辦法就是人工將name屬性改為統(tǒng)一的,那么如何改呢?有的人可能會說那很簡單啊,使用GridView的OnRowDataBound事件在行綁定的時(shí)候講RadioButton的name屬性改一下就好拉!代碼如下:
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- RadioButton RadioButtionRB = (RadioButton)e.Row.FindControl("rbStudent");
- RadioButtionRB.Attributes["name"] = "student";
- }
- }
- <span name="student"><input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" /></span>
- </td><td>張三</td><td>男</td>
- </tr><tr>
- <td>
- <span name="student"><input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" /></span>
- </td><td>李四</td><td>女</td>
- </tr><tr>
- <td>
- <span name="student"><input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" /></span>
- </td><td>王五</td><td>男</td>
② 問題解決
首先先來實(shí)現(xiàn)一個(gè)JS函數(shù),用來獲取遍歷GridView中的RadioButton并將其name屬性設(shè)置為一個(gè)統(tǒng)一的值,例如“myradio”,代碼如下:
- <script type="text/javascript">
- function SetRadioName() {
- var gv = document.getElementById("GridView1"); //獲取GridView的客戶端ID
- var myradio = gv.getElementsByTagName("input"); //獲取GridView的Inputhtml
- for (var i = 0; i < myradio.length; i++) {
- if (myradio[i].type == 'radio')//hidden
- {
- myradio[i].setAttribute("name", "myradio");
- }
- }
- }
- </script>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- this.BindGridView();
- ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
- "SetRadioName()", true);
- }
- }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。