溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

asp.net GridView中如何使用RadioButton單選按鈕

發(fā)布時(shí)間:2021-08-27 14:07:53 來源:億速云 閱讀:200 作者:小新 欄目:開發(fā)技術(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)常要用的是選中的行號,有了行號,我們就可以取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)槭褂玫氖强蛻舳丝丶赃x中的值不可以寫入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;'>&nbsp;&nbsp;" + per.ToString() + "%&nbsp;&nbsp;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;'>&nbsp;&nbsp;5%&nbsp;&nbsp;5票</div>
</label>
</td>
</tr>

div是塊級元素,使用了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)典
}

看完了這篇文章,相信你對“asp.net GridView中如何使用RadioButton單選按鈕”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(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)容。

AI