获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList:

<asp:RadioButtonList ID="rbl" runat="server">
<asp:ListItem Value="1">单选1</asp:ListItem>
<asp:ListItem Value="2">单选2</asp:ListItem>
<asp:ListItem Value="3">单选3</asp:ListItem>
</asp:RadioButtonList>

后台通过 this.rbl.SelectedValue 来获取选则的值,关于RadioButtonList的使用,这里就不介绍了。

由于RadioButtonList的排列是有规则的,不管是横排还是竖排,紧凑还是对齐。

那么,如果是一组没有规则的RadioButton,那么这些值怎么获取呢?

<asp:RadioButton ID="rb1" runat="server" GroupName="new" Text="单选1" />
<div>……</div>
<asp:RadioButton ID="rb2" runat="server" GroupName="new" Text="单选2" />
<div>……</div>
<asp:RadioButton ID="rb3" runat="server" GroupName="new" Text="单选3" />
<div>……</div>

办法当然有很多,比如最常用的:当选中值的时候,用JS将值放入页面的隐藏控件中,再在后台获取隐藏控件的值。

这里介绍另外一种方法,比如页面禁用JS的时候,这种方法就有效了:

首先,在涵盖所有需要取值的RadioButton外面,加一个div,并且runat="server"

<div id="div1" runat="server">
<asp:RadioButton ID="rb1" runat="server" GroupName="new" Text="单选1" />
<div>……</div>
<asp:RadioButton ID="rb2" runat="server" GroupName="new" Text="单选2" />
<div>……</div>
<asp:RadioButton ID="rb3" runat="server" GroupName="new" Text="单选3" />
</div>
<div>……</div>

然后,后台写一个这样的方法:

public string GetRadioButtonGroupValue(Control ctrl, string controlName)
{
   foreach (Control control in ctrl.Controls)
   {
      if (control is RadioButton)
      {
         RadioButton lControl = control as RadioButton;
         if (lControl.Checked && lControl.GroupName == controlName)
         {
            return lControl.Text;
         }
      }
   }
return null;
}

最后,调用就可以了:

GetRadioButtonGroupValue(this.div1, "new")

后台获取不规则排列RadioButton组的值的更多相关文章

  1. asp.net后台获取html控件的值

    1.asp.net后台获取前台type=text控件的值 前台:<input name="txtName" class="username" type=& ...

  2. 在后台获取Textarea控件的值

    使用Request.Form方法 1.在前台设置name 属性 <textarea name="Content">hdjfhjdfhdj</textarea> ...

  3. 使用jQuery获取radio/checkbox组的值的代码收集

    <!-- $("document").ready(function(){ $("#btn1").click(function(){ $("[na ...

  4. 后台获取ajax发送过来的值

    user.CityId = int.Parse(HttpContext.Request[ "bindArea"]); 以上为获取方法:

  5. 后台获取html控件的值

    string name = Request.Form["Name1"].ToString(); “xxx” 里的是name值 Request["xx"]取到相同 ...

  6. 【ADO.NET基础】后台获取前台控件

    C# 后台获取前台 input 文本框值.string aa=Request.Form[headself]; 那么要是后台给前台input文本框赋值呢? 后台 public string Headse ...

  7. javaWeb - 2 — ajax、json — 最后附:后台获取前端中的input type = "file"中的信息 — 更新完毕

    1.ajax是什么? 面向百度百科一下就知道了,这里就简单提炼一下 Ajax即Asynchronous Javascript And XML(异步JavaScript和XML).当然其实我们学的应该叫 ...

  8. .net后台获取HTML中select元素选中的值

    前台: <select id="Province" name="Province" class="select"></se ...

  9. Hbuilder app开发,使用mui.ajax和服务器交互,后台获取不到值,显示null的解决方法

    先上一个能用的js代码: function login() { var uname=document.getElementById("username").value.trim() ...

随机推荐

  1. python 键值对的树实现

    #coding:utf-8 __author__ = 'similarface' class KeyedBinaryTree: def __init__(self):self.tree=EmptyNo ...

  2. 什么是cname a记录

    https://support.dnsimple.com/articles/cname-record/ CNAME就是别名记录,就是负责跳转,比如你给某个地址设置了一个cname,那当访问那个cnam ...

  3. VB6 GDI+ 入门教程[5] 基础绘图小结

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[5] 基础绘图小结 2009 年 6 月 18 日 4条 ...

  4. VB6 GDI+ 入门教程[6] 图片

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[6] 图片 2009 年 6 月 19 日 15条评论 ...

  5. 集成骨骼动画Spine的几点经验

    最近开始用cantk做些复杂的游戏,其中一个游戏的DragonBones骨骼动画的JSON文件就达600K,导出之后显示各种不正常,可能是太复杂了,有些方面达到了DragonBones的极限.拿到官方 ...

  6. 【图形学】我理解的伽马校正(Gamma Correction)

    http://blog.csdn.net/candycat1992/article/details/46228771/ 写在前面 我相信几乎所有做图像处理方面的人都听过伽马校正(Gamma Corre ...

  7. sqlserver计算表使用大小sql

    ) create table #spt_space ( ) null, [rows] int null, ) null, ) null, ) null, ) null ) set nocount on ...

  8. 浅谈线程池(中):独立线程池的作用及IO线程池

    原文地址:http://blog.zhaojie.me/2009/07/thread-pool-2-dedicate-pool-and-io-pool.html 在上一篇文章中,我们简单讨论了线程池的 ...

  9. React添加事件

    定义个组件 组件首字母大写,调用: ReactDOM.render(<Hello></Hello>,document.getElementById('box'));

  10. js控制手机号码中间用星号代替

    $("#tel").html($("#tel").substring(0,3)+"****"+$("#tel").sub ...