服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net GridView中使用RadioButton单选按钮的方法

asp.net GridView中使用RadioButton单选按钮的方法

2020-01-21 14:18wdfscp ASP.NET教程

这篇文章主要介绍了asp.net GridView中使用RadioButton单选按钮的方法,结合实例形式总结分析了三种GridView中使用RadioButton单选按钮的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

延伸 · 阅读

精彩推荐