asp.net GridView中使用RadioButton單選按鈕的方法
來源:易賢網(wǎng) 閱讀:958 次 日期:2016-08-05 15:09:22
溫馨提示:易賢網(wǎng)小編為您整理了“asp.net GridView中使用RadioButton單選按鈕的方法”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了asp.net GridView中使用RadioButton單選按鈕的方法。分享給大家供大家參考,具體如下:

在GridView里做單選按鈕,我用了三種方法

第一種方法:在GridView的模版列里加服務(wù)器端控件RadioButton,使用js控制單選

使用模版列里加RadioButton

<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>

--------------------------------------------------------

<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="取選項(xiàng)" OnClick="Button1_Click"/>

<asp:Label ID="Label1" runat="server"></asp:Label>

前面那段代碼就是控制單選的js,在這里,我使用了遍歷頁面上所有控件的方法,加入了條件,就是紅色那個(gè)判斷,只控制GridView1里id是RadioButton1生成的單選按鈕

這種辦法需要綁定客戶端事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//給每個(gè)RadioButton1綁定setRadio事件

try

{

((RadioButton)e.Row.FindControl("RadioButton1")).Attributes.Add("onclick", "setRadio(this)");

}

catch (Exception)

{ }

}

取值的方法就是遍歷GridView的每一行,取選中的控件

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 = "當(dāng)前選中第" + Convert.ToString(gvr.RowIndex + 1) + "個(gè)";

break;

}

}

catch (Exception)

{ }

}

if (Label1.Text.Length == 0)

{

Label1.Text = "沒有選中項(xiàng)";

}

}

這種方法,在客戶端和服務(wù)器端都使用了遍歷

第二種方法:在GridView的模版列里,加html控件Radio

使用模版列里加html控件Radio

<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="取選項(xiàng)" OnClick="Button2_Click" />

<asp:Label ID="Label2" runat="server"></asp:Label>

-----------------------------------------------------

<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值里,綁定的是當(dāng)前行,因?yàn)橐话阍贕ridView里操作的時(shí)候,我們經(jīng)常要用的是選中的行號(hào),有了行號(hào),我們就可以取GridView的DataKeys了

因?yàn)檫@里使用的是html控件,所以取數(shù)據(jù)的時(shí)候,要使用Request.Form

protected void Button2_Click(object sender, EventArgs e)

{

//使用模版列里加html控件Radio

if (Request.Form["myRadio"] == null)

{

Label2.Text = "沒有選中項(xiàng)";

jsLiteral.Text = "";//*****

}

else

{

string value;

value = Request.Form["myRadio"].ToString();

Label2.Text = "當(dāng)前選中第" + Convert.ToString(Convert.ToInt16(value) + 1) + "個(gè)";

jsLiteral.Text = "setNowRadio('" + value + "');";//*****

}

}

這種方法自己,是不用遍歷控件就可以完成任務(wù)的

就是因?yàn)槭褂玫氖强蛻舳丝丶?,所以選中的值不可以寫入viewstate里面,如果有頁面回傳,這個(gè)值就不可以保留了,如果要在頁面回傳后還保留這個(gè)值,就要使用js,看注釋里有****的那段代碼,我選設(shè)置了一個(gè)setNowRadio(),然后呢加入Literal控件

在每一次回傳的時(shí)候,嗯,因?yàn)槲疫@里只有取值需要回傳,所以我寫在了取值那里,其實(shí)是應(yīng)該寫在Page_Load事件里的,加上if (IsPostBack)的判斷,就是每次回傳,就要取這個(gè)myRadio的值,執(zhí)行函數(shù),重新選擇已經(jīng)選中的項(xiàng)

在這個(gè)setNowRadio里,又用到了遍歷,就是他比第一種方法遍歷的東西少

第三種方法:直接使用RadioButtonList模擬表格

使用RadioButtonList

<asp:RadioButtonList ID="RadioButtonList1" runat="server">

</asp:RadioButtonList>

<asp:Button ID="Button3" runat="server" Text="取選項(xiàng)" OnClick="Button3_Click" />

<asp:Label ID="Label3" runat="server"></asp:Label>

我在這里模擬的是一個(gè)像論壇里,顯示投票頁面的東西,就是給出一個(gè)單選框,后面寫選項(xiàng)內(nèi)容,然后是一個(gè)圖片,再顯示有幾票

private void SetListItem(RadioButtonList rbt)

{

//給RadioButtonList加幾個(gè)ListItem,用來測試數(shù)據(jù)

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) + " 項(xiàng)</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, ""));

}

}

這種方法解決了單選的問題,解決了回傳的問題,因?yàn)镽adioButtonList本來就是生成一組Radio控件的,就是,在模擬的時(shí)候很麻煩,我這里使用了很多div+css,就是,我還是沒有辦法做到讓生成的radio和選項(xiàng)放在同一行上

下面是生成的html代碼里的一行:

<tr>

<td>

<input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="" />

<label for="RadioButtonList1_0">

<div style='float:left; width:300px;'> 第 1 項(xiàng)</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是塊級(jí)元素,使用了float:left,也不可以讓他們和radio在同一行上,如果可以把頁面的寬度控制,比如確定是788px,那我們就可以使用float:right; text-align:left;來控制,就是很多時(shí)候,是不允許用px控制頁面寬度的

另外的一個(gè)辦法是直接寫代碼

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;//經(jīng)典

}

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:asp.net GridView中使用RadioButton單選按鈕的方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)