本文實例講述了asp.net GridView中使用RadioButton單選按鈕的方法。分享給大家供大家參考,具體如下:
在GridView里做單選按鈕,我用了三種方法
第一種方法:在GridView的模版列里加服務器端控件RadioButton,使用js控制單選
使用模版列里加RadioButton
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<script type= "text/javascript" > function setRadio(nowRadio) { var myForm,objRadio; myForm=document.forms[0]; /**/ ///alert(myForm); for ( var i=0;i<myForm.length;i++) { if (myForm.elements[i].type== "radio" ) { objRadio=myForm.elements[i]; /**/ ///alert(objRadio.name); if (objRadio!=nowRadio && objRadio.name.indexOf( "GridView1" )>-1 && objRadio.name.indexOf( "RadioButton1" )>-1) { alert(objRadio.name); if (objRadio.checked) { objRadio.checked= false ; } } } } } </script> |
1
2
3
4
5
6
7
8
9
10
11
|
<asp:GridView ID= "GridView1" runat= "server" AutoGenerateColumns= "False" ShowHeader= "False" OnRowDataBound= "GridView1_RowDataBound" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:RadioButton ID= "RadioButton1" runat= "server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID= "Button1" runat= "server" Text= "取選項" OnClick= "Button1_Click" /> <asp:Label ID= "Label1" runat= "server" ></asp:Label> |
前面那段代碼就是控制單選的js,在這里,我使用了遍歷頁面上所有控件的方法,加入了條件,就是紅色那個判斷,只控制GridView1里id是RadioButton1生成的單選按鈕
這種辦法需要綁定客戶端事件
1
2
3
4
5
6
7
8
9
10
|
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) { //給每個RadioButton1綁定setRadio事件 try { ((RadioButton)e.Row.FindControl( "RadioButton1" )).Attributes.Add( "onclick" , "setRadio(this)" ); } catch (Exception) { } } |
取值的方法就是遍歷GridView的每一行,取選中的控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
protected void Button1_Click( object sender, EventArgs e) { //使用模版列里加RadioButton Label1.Text = "" ; foreach (GridViewRow gvr in GridView1.Rows) { try { if (((RadioButton)gvr.FindControl( "RadioButton1" )).Checked) { Label1.Text = "當前選中第" + Convert.ToString(gvr.RowIndex + 1) + "個" ; break ; } } catch (Exception) { } } if (Label1.Text.Length == 0) { Label1.Text = "沒有選中項" ; } } |
這種方法,在客戶端和服務器端都使用了遍歷
第二種方法:在GridView的模版列里,加html控件Radio
使用模版列里加html控件Radio
1
2
3
4
5
6
7
8
9
10
11
|
<asp:GridView ID= "GridView2" runat= "server" AutoGenerateColumns= "False" ShowHeader= "False" > <Columns> <asp:TemplateField> <ItemTemplate> <input type= "radio" name= "myRadio" value= '<%# Container.DataItemIndex.ToString() %>' > </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:Button ID= "Button2" runat= "server" Text= "取選項" OnClick= "Button2_Click" /> <asp:Label ID= "Label2" runat= "server" ></asp:Label> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<script type= "text/javascript" > function setNowRadio(v) { //alert(v); var myForm,objRadio; myForm=document.forms[0]; for ( var i=0;i<myForm.length;i++) { if (myForm.elements[i].type== "radio" ) { objRadio=myForm.elements[i]; //alert(objRadio.name); //alert(objRadio.value); if (objRadio.value==v) { objRadio.checked= true ; } } } } <asp:Literal ID= "jsLiteral" runat= "server" ></asp:Literal> </script> |
前面那句<input type="radio" name="myRadio" value='<%# Container.DataItemIndex.ToString() %>'>,我在他的value值里,綁定的是當前行,因為一般在GridView里操作的時候,我們經常要用的是選中的行號,有了行號,我們就可以取GridView的DataKeys了
因為這里使用的是html控件,所以取數據的時候,要使用Request.Form
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
protected void Button2_Click( object sender, EventArgs e) { //使用模版列里加html控件Radio if (Request.Form[ "myRadio" ] == null ) { Label2.Text = "沒有選中項" ; jsLiteral.Text = "" ; //***** } else { string value; value = Request.Form[ "myRadio" ].ToString(); Label2.Text = "當前選中第" + Convert.ToString(Convert.ToInt16(value) + 1) + "個" ; jsLiteral.Text = "setNowRadio('" + value + "');" ; //***** } } |
這種方法自己,是不用遍歷控件就可以完成任務的
就是因為使用的是客戶端控件,所以選中的值不可以寫入viewstate里面,如果有頁面回傳,這個值就不可以保留了,如果要在頁面回傳后還保留這個值,就要使用js,看注釋里有****的那段代碼,我選設置了一個setNowRadio(),然后呢加入Literal控件
在每一次回傳的時候,嗯,因為我這里只有取值需要回傳,所以我寫在了取值那里,其實是應該寫在Page_Load事件里的,加上if (IsPostBack)的判斷,就是每次回傳,就要取這個myRadio的值,執行函數,重新選擇已經選中的項
在這個setNowRadio里,又用到了遍歷,就是他比第一種方法遍歷的東西少
第三種方法:直接使用RadioButtonList模擬表格
使用RadioButtonList
1
2
3
4
|
<asp:RadioButtonList ID= "RadioButtonList1" runat= "server" > </asp:RadioButtonList> <asp:Button ID= "Button3" runat= "server" Text= "取選項" OnClick= "Button3_Click" /> <asp:Label ID= "Label3" runat= "server" ></asp:Label> |
我在這里模擬的是一個像論壇里,顯示投票頁面的東西,就是給出一個單選框,后面寫選項內容,然后是一個圖片,再顯示有幾票
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private void SetListItem(RadioButtonList rbt) { //給RadioButtonList加幾個ListItem,用來測試數據 string item, space, info; int per; for ( int i = 0; i < 3; i++) { per = 5; item = "<div style='float:left; width:300px;'> 第 " + Convert.ToString(i + 1) + " 項</div>" ; space = Convert.ToString(per * 3.50); space = "<div style='float:left; background-color:MistyRose;border-color:Silver;border-width:1px;border-style:solid; width:" + space + "px;'></div>" ; info = "<div style='float:left; width:70px;'> " + per.ToString() + "% 5票</div>" ; info = item + space + info; RadioButtonList1.Items.Add( new ListItem(info, "" )); } } |
這種方法解決了單選的問題,解決了回傳的問題,因為RadioButtonList本來就是生成一組Radio控件的,就是,在模擬的時候很麻煩,我這里使用了很多div+css,就是,我還是沒有辦法做到讓生成的radio和選項放在同一行上
下面是生成的html代碼里的一行:
1
2
3
4
5
6
7
8
9
10
|
< tr > < td > < input id = "RadioButtonList1_0" type = "radio" name = "RadioButtonList1" value = "" /> < label for = "RadioButtonList1_0" > < div style = 'float:left; width:300px;' > 第 1 項</ div > < div style = 'float:left; background-color:MistyRose;border-color:Silver;border-width:1px;border-style:solid; width:17.5px;' ></ div > < div style = 'float:left; width:70px;' > 5% 5票</ div > </ label > </ td > </ tr > |
div是塊級元素,使用了float:left,也不可以讓他們和radio在同一行上,如果可以把頁面的寬度控制,比如確定是788px,那我們就可以使用float:right; text-align:left;來控制,就是很多時候,是不允許用px控制頁面寬度的
另外的一個辦法是直接寫代碼
1
2
3
4
5
6
7
8
|
protected void rbtnSel_CheckedChanged( object sender, EventArgs e) { for ( int i = 0; i < this .GridView1.Rows.Count; i++) { ((RadioButton) this .GridView1.Rows[i].FindControl( "rbtnSel" )).Checked = false ; } ((RadioButton)sender).Checked = true ; //經典 } |
希望本文所述對大家asp.net程序設計有所幫助。