解決ASP.NET GridView中加入RadioButton不能單選

         今天開發碰見一個問題,就是當GridView中加入一個包含RadioButton的模板列,結果一運行。。。。。天啊,單選按鈕可以多選了! 囧啊!爲了演示一下我今天的錯誤我還是模擬一個功能場景吧,我要實現的功能是顯示一個包含單選按鈕的學生信息列表,選擇一行後將詳細信息顯示出來~!

          1.問題展現

              ①首先準備一個GridView用來展示學生的基本信息與最重要的單選按鈕,代碼如下:

  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> 
  2.         <Columns> 
  3.         <asp:TemplateField> 
  4.           <ItemTemplate> 
  5.           <asp:RadioButton ID="rbStudent" runat="server" /> 
  6.           </ItemTemplate> 
  7.         </asp:TemplateField> 
  8.  
  9.         <asp:BoundField DataField="SName" HeaderText="用戶名"  /> 
  10.         <asp:BoundField DataField="SSex" HeaderText="性別" /> 
  11.         </Columns> 
  12.         </asp:GridView> 

              ②接下來準備需要綁定數據的實體,代碼如下:

  1. public class Student  
  2.   {  
  3.       public string SID { getset; }  
  4.       public string SName { getset; }  
  5.       public string SSex { getset; }  
  6.   } 

              ③初始化數據,綁定GridView列表,代碼如下:

  1. protected void Page_Load(object sender, EventArgs e)  
  2.        {  
  3.            if (!IsPostBack)  
  4.            {  
  5.                this.BindGridView();  
  6.            }  
  7.        }  
  8.  
  9.        public void BindGridView()  
  10.        {  
  11.            List<Student> sList = new List<Student>()  
  12.            {  
  13.                new Student(){ SID = "s001", SName="張三", SSex="男"},  
  14.                new Student(){ SID = "s002", SName="李四", SSex="女"},  
  15.                new Student(){ SID = "s003", SName="王五", SSex="男"}  
  16.            };  
  17.  
  18.            GridView1.DataSource = sList;  
  19.            GridView1.DataBind();  
  20.        }  
           這個時候看起來沒有什麼問題,但是一運行我確發現,單選框失去了本身的作用,如圖:

        

         什麼原因呢?細心的人可能會說RadioButton你沒有設置GroupName屬性所以不屬於一組,但事實我設置了該屬性後還是無效!

          2.解決思路

           ① 問題分析

              在運行後,我右鍵查看源代碼後發現了這個詭異的問題,原來是GridView會自動給input 的name屬性賦值,導致了每一行的name屬性都不一樣,造成了不能單選的問題,GridView生成代碼如下:

  1. <table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;"> 
  2.         <tr> 
  3.             <th scope="col">&nbsp;</th><th scope="col">用戶名</th><th scope="col">性別</th> 
  4.         </tr><tr> 
  5.             <td> 
  6.           <input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" /> 
  7.           </td><td>張三</td><td></td> 
  8.         </tr><tr> 
  9.             <td> 
  10.           <input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" /> 
  11.           </td><td>李四</td><td></td> 
  12.         </tr><tr> 
  13.             <td> 
  14.           <input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" /> 
  15.           </td><td>王五</td><td></td> 
  16.         </tr> 
  17.     </table> 
             可以發現每一個RadioButton控件的name屬性都不一樣,導致了不能單選的問題,那麼如何解決掉這個罪魁禍首呢?

              我第一個想到的辦法就是人工將name屬性改爲統一的,那麼如何改呢?有的人可能會說那很簡單啊,使用GridView的OnRowDataBound事件在行綁定的時候講RadioButton的name屬性改一下就好拉!代碼如下:

  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2.        {  
  3.            if (e.Row.RowType == DataControlRowType.DataRow)  
  4.            {  
  5.                RadioButton RadioButtionRB = (RadioButton)e.Row.FindControl("rbStudent");  
  6.                RadioButtionRB.Attributes["name"] = "student";  
  7.            }  
  8.        }  
               但是運行後,我又失望了,什麼原因呢? 是因爲RadioButton在客戶端輸出的時候外面會有一層<SPAN>標記,name屬性被加到SPAN上面了,囧死了。證據如下:
  1. <span name="student"><input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" /></span> 
  2.           </td><td>張三</td><td></td> 
  3.         </tr><tr> 
  4.             <td> 
  5.           <span name="student"><input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" /></span> 
  6.           </td><td>李四</td><td></td> 
  7.         </tr><tr> 
  8.             <td> 
  9.           <span name="student"><input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" /></span> 
  10.           </td><td>王五</td><td></td> 
             看來這種思路行不通啊,只能用JS啦,所以正題來了,我決定在數據綁定後通過JS遍歷GridView中的RadioButton將name屬性改爲相同的值,行得通嗎?試試看唄!          

           ② 問題解決

               首先先來實現一個JS函數,用來獲取遍歷GridView中的RadioButton並將其name屬性設置爲一個統一的值,例如“myradio”,代碼如下:

  1. <script type="text/javascript">  
  2.        function SetRadioName() {  
  3.            var gv = document.getElementById("GridView1"); //獲取GridView的客戶端ID  
  4.            var myradio = gv.getElementsByTagName("input"); //獲取GridView的Inputhtml  
  5.            for (var i = 0; i < myradio.length; i++) {  
  6.                if (myradio[i].type == 'radio')//hidden  
  7.                {  
  8.                    myradio[i].setAttribute("name""myradio");  
  9.                }  
  10.            }  
  11.        }  
  12.    </script>  
              接下來在綁定數據後註冊調用這段腳本,或者將該腳本寫到頁面標籤視圖的最下面,代碼如下:
  1. protected void Page_Load(object sender, EventArgs e)  
  2.        {  
  3.            if (!IsPostBack)  
  4.            {  
  5.                this.BindGridView();  
  6.                ScriptManager.RegisterStartupScript(thisthis.GetType(), Guid.NewGuid().ToString(),  
  7.                    "SetRadioName()"true);  
  8.            }  
  9.        }  
             運行,成功!!!!!!完美解決!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章