asp:RadioButtonList默认值

网上看到好多设置初始值方法,总结保存一下,以后自己还用得到

1. jquary设置

网页地址:[http://blog.csdn.net/pengdayong77/article/details/50544856](http://blog.csdn.net/pengdayong77/article/details/50544856)
此方法原理是查看asp页面生成为html后的值,然后依据生成后的id等设置初始值,我没有试过,应该可以实现,但是不建议使用,毕竟是野路子,此处使用了jquary,使用原生js同理
asp代码:
<asp:RadioButtonList ID="rbn_list" runat="server">  
      <asp:ListItem Text="Red" Value="10"></asp:ListItem>  
       <asp:ListItem Text="Blue" Value="20"></asp:ListItem>  
   </asp:RadioButtonList> 
生成html后代码:
<pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><table id="rbn_list" border="0" __id="19">  
    <tbody><tr __id="20">  
        <td __id="21"><input id="rbn_list_0" name="rbn_list" value="10" type="radio" __id="22"><label for="rbn_list_0" __id="23">Red</label></td>  
    </tr><tr __id="24">  
        <td __id="25"><input id="rbn_list_1" name="rbn_list" value="20" type="radio" __id="26"><label for="rbn_list_1" __id="27">Blue</label></td>  
    </tr>  
</tbody></table> 
 由以上可以看出微软自动生成了一个名字叫做rbn_list的控件集。并在它的后面生成一个标签。用于存放radioButton的值。因此如果要取得该控件的集的值的方法是
    <span style="font-family:Arial, Helvetica, sans-serif;"></span><pre name="code" class="javascript"> $("[name=rbn_list]").each(function (index, obj) {  
                alert($(this).val());  
                //alert($(this).next().text());  
            }) 
因此如果要取得该控件的集的名称值的方法是
    <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="javascript"> $("[name=rbn_list]").each(function (index, obj) {  
                alert($(this).next().text());  
            })
设置该控件被选择中的方法是
    $("[name=rbn_list]").eq(1).prop("checked", "true"); 

或者

<pre name="code" class="javascript">var choise = $("[name=rbn_list][value=10]");  
            choise.prop("checked","true"); 

2. 默认设置第一个选项

网页地址:http://www.webmaster5u.com/show.asp?id=2805
我在做设置默认值时有使用过这个方法,此方法可用,但是有两个小问题
第一就是需要在后台写代码,如果前端有修改的话还需要重新生成有点麻烦
第二还是如果前端对于ListItem的值的顺序有修改,还是需要修改后台,而且需要对应,麻烦!
代码:

this.radioButtonList.Items[0].Selected = true; 

这样就可以设置第一个默认选中,同理设置第二个默认(radioButtonList为ID)

this.radioButtonList.Items[1].Selected = true; 

如果用这种方法设置某个值默认的话:

for (int i = 0; i < this.cblist_type.Items.Count; i++)
                {
                    if (this.cblist_type.Items[i].Text =="姓名")
                    {
                        this.cblist_type.Items[i].Selected = true;
                    }
                }

还需要再用循环,是不是很麻烦

3. 设置某个值

地址:https://zhidao.baidu.com/question/118922353.html
我搜到好几个说用这种方法的,但是自己并没有成功,可能是我的项目的asp版本太低了
,所以不推荐使用这种
在初始化界面的这个语句下:

private void Form1_Load(object sender, EventArgs e)
{
    this.radioButton1.Checked = true;
}  

还有一种成功可以使用的

 this.radioButton1.SelectedValue = “abc”;

看情况使用吧

4. 前端设置

地址:无
这个方法是看前辈的代码找到的,因为是接收的完成的项目做二开,所以看了看之前是怎么做的,感觉这个方法不错,当然具体情况还需要再考虑用哪一种了
代码:

<asp:RadioButtonList ID="ApplicationType" runat="server" RepeatDirection="Horizontal" CssClass="nobordertable">
    <asp:ListItem Text="无立项" Value="无立项" Selected="True"></asp:ListItem>
    <asp:ListItem Text="有立项" Value="有立项"></asp:ListItem>
</asp:RadioButtonList>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值