ASP.NET GridView中加入RadioButton不能單選的解決方案
今天開(kāi)發(fā)碰見(jiàn)一個(gè)問(wèn)題,就是當(dāng)GridView中加入一個(gè)包含RadioButton的模板列,結(jié)果一運(yùn)行。。。。。天啊,單選按鈕可以多選了! 囧?。榱搜菔疽幌挛医裉斓腻e(cuò)誤我還是模擬一個(gè)功能場(chǎng)景吧,我要實(shí)現(xiàn)的功能是顯示一個(gè)包含單選按鈕的學(xué)生信息列表,選擇一行后將詳細(xì)信息顯示出來(lái)~!
1.問(wèn)題展現(xiàn)
①首先準(zhǔn)備一個(gè)GridView用來(lái)展示學(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="用戶(hù)名" />
<asp:BoundField DataField="SSex" HeaderText="性別" />
</Columns>
</asp:GridView>
②接下來(lái)準(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();
}
這個(gè)時(shí)候看起來(lái)沒(méi)有什么問(wèn)題,但是一運(yùn)行我確發(fā)現(xiàn),單選框失去了本身的作用,如圖:

什么原因呢?細(xì)心的人可能會(huì)說(shuō)RadioButton你沒(méi)有設(shè)置GroupName屬性所以不屬于一組,但事實(shí)我設(shè)置了該屬性后還是無(wú)效!
2.解決思路
① 問(wèn)題分析
在運(yùn)行后,我右鍵查看源代碼后發(fā)現(xiàn)了這個(gè)詭異的問(wèn)題,原來(lái)是GridView會(huì)自動(dòng)給input 的name屬性賦值,導(dǎo)致了每一行的name屬性都不一樣,造成了不能單選的問(wèn)題,GridView生成代碼如下:
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col"> </th><th scope="col">用戶(hù)名</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>
可以發(fā)現(xiàn)每一個(gè)RadioButton控件的name屬性都不一樣,導(dǎo)致了不能單選的問(wèn)題,那么如何解決掉這個(gè)罪魁禍?zhǔn)啄兀?/p>
我第一個(gè)想到的辦法就是人工將name屬性改為統(tǒng)一的,那么如何改呢?有的人可能會(huì)說(shuō)那很簡(jiǎn)單啊,使用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";
}
}
但是運(yùn)行后,我又失望了,什么原因呢? 是因?yàn)镽adioButton在客戶(hù)端輸出的時(shí)候外面會(huì)有一層<SPAN>標(biāo)記,name屬性被加到SPAN上面了,囧死了。證據(jù)如下:
<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>
看來(lái)這種思路行不通啊,只能用JS啦,所以正題來(lái)了,我決定在數(shù)據(jù)綁定后通過(guò)JS遍歷GridView中的RadioButton將name屬性改為相同的值,行得通嗎?試試看唄!
② 問(wèn)題解決
首先先來(lái)實(shí)現(xiàn)一個(gè)JS函數(shù),用來(lái)獲取遍歷GridView中的RadioButton并將其name屬性設(shè)置為一個(gè)統(tǒng)一的值,例如“myradio”,代碼如下:
<script type="text/javascript">
function SetRadioName() {
var gv = document.getElementById("GridView1"); //獲取GridView的客戶(hù)端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>
接下來(lái)在綁定數(shù)據(jù)后注冊(cè)調(diào)用這段腳本,或者將該腳本寫(xiě)到頁(yè)面標(biāo)簽視圖的最下面,代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
"SetRadioName()", true);
}
}
問(wèn)題解決了!
經(jīng)過(guò)一番努力問(wèn)題終于得到完美解決,整個(gè)問(wèn)題分析思路清晰,希望可以真正幫助到親們解決類(lèi)似問(wèn)題,有好的想法,歡迎大家一起探討
- Android利用GridView實(shí)現(xiàn)單選功能
- asp.net GridView中使用RadioButton單選按鈕的方法
- gridview中實(shí)現(xiàn)radiobutton的單選示例
- DataGridView中CheckBox實(shí)現(xiàn)某一列單選
- Gridview使用CheckBox全選與單選采用js實(shí)現(xiàn)同時(shí)高亮顯示選擇行
- js實(shí)現(xiàn)GridView單選效果自動(dòng)設(shè)置交替行、選中行、鼠標(biāo)移動(dòng)行背景色
- asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼
- Android利用GridView實(shí)現(xiàn)單選效果
相關(guān)文章
ASP.NET頁(yè)面間數(shù)據(jù)傳遞的幾種方法介紹
在ASP.NET中,頁(yè)面間數(shù)據(jù)傳遞的方法有很多。下面為大家總結(jié)一下,頁(yè)面間數(shù)據(jù)傳遞的方法,來(lái)看作者的分析。2013-05-05
ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例
這篇文章主要介紹了ASP.NET中 Execl導(dǎo)出的六種方法實(shí)例,有需要的朋友可以參考一下2013-12-12
在Asp.net中為圖像加入水印信息并保存為Jpg類(lèi)型
這篇文章主要介紹了在Asp.net中為圖像加入水印信息,可定義字體、筆刷等等并保存為Jpg類(lèi)型,需要的朋友可以參考下2014-08-08
ASP.NET 2.0 程序安全的基礎(chǔ)知識(shí)
成員關(guān)系的概念在人類(lèi)社會(huì)中是一個(gè)層次比較低的概念,源于希望屬于某個(gè)群組的意識(shí)。同樣,在ASP.NET 2.0程序開(kāi)始開(kāi)發(fā)涉及到成員關(guān)系的應(yīng)用程序時(shí),必須首先理解身份、驗(yàn)證和授權(quán)這幾個(gè)關(guān)鍵的概念。2010-04-04
使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移
這篇文章介紹了使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
瀏覽器窗口滾動(dòng)加載數(shù)據(jù)采用異步形式從后臺(tái)加載數(shù)據(jù)
在滾動(dòng)條距頂部距離(頁(yè)面超出窗口的高度)時(shí)采用異步形式從后臺(tái)加載數(shù)據(jù),下面是具體的實(shí)現(xiàn),希望對(duì)大家有所幫助2014-01-01
asp.net iis 無(wú)法顯示網(wǎng)頁(yè)的解決方法分析
使用過(guò)IIS的朋友都可能遇到過(guò)這樣的情況:即使您按照教科書(shū)的步驟做好各步設(shè)置以后,仍會(huì)出現(xiàn)“無(wú)法顯示網(wǎng)頁(yè)”的現(xiàn)象。2010-06-06

