在GridView里使用单选按钮,RadioButton/Radio

本文介绍三种在ASP.NET GridView中实现单选功能的方法:使用服务器控件RadioButton、客户端控件Radio及RadioButtonList控件,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c

p>使用模版列里加RadioButton</p>         <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="取选项" OnClick="Button1_Click" />         <asp:Label ID="Label1" runat="server"></asp:Label>

蓝色那段就是控制单选的js,在这里,我使用了遍历页面上所有控件的方法,加入了条件,就是红色那个判断,只控制GridView1里id是RadioButton1生成的单选按钮

这种办法需要绑定客户端事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)     {         //给每个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 = "当前选中第" + Convert.ToString(gvr.RowIndex + 1) + "个";                     break;                 }             }             catch (Exception)             { }         }         if (Label1.Text.Length == 0)         {             Label1.Text = "没有选中项";         }     }

这种方法,在客户端和服务器端都使用了遍历

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

<p>使用模版列里加html控件Radio</p>         <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>         <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>

就是蓝色那句,我在他的value值里,绑定的是当前行,因为一般在GridView里操作的时候,我们经常要用的是选中的行号,有了行号,我们就可以取GridView的DataKeys了

因为这里使用的是html控件,所以取数据的时候,要使用Request.Form

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模拟表格

<p>使用RadioButtonList</p>         <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>

我在这里模拟的是一个像论坛里,显示投票页面的东西,就是给出一个单选框,后面写选项内容,然后是一个图片,再显示有几票

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;'>&nbsp;&nbsp;" + per.ToString() + "%&nbsp;&nbsp;5票</div>";             info = item + space + info;             RadioButtonList1.Items.Add(new ListItem(info, ""));         }     }

这种方法解决了单选的问题,解决了回传的问题,因为RadioButtonList本来就是生成一组Radio控件的,就是,在模拟的时候很麻烦,我这里使用了很多div+css,就是,我还是没有办法做到让生成的radio和选项放在同一行上

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

<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;'>&nbsp;&nbsp;5%&nbsp;&nbsp;5票</div>         </label>     </td> </tr>

div是块级元素,使用了float:left,也不可以让他们和radio在同一行上,如果可以把页面的宽度控制,比如确定是788px,那我们就可以使用float:right; text-align:left;来控制,就是很多时候,是不允许用px控制页面宽度的

另外的一个办法是直接写代码

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;//经典             }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值