asp:RadioButtonList默认值

来源:互联网 发布:淘宝号可以不实名认证 编辑:程序博客网 时间:2024/06/05 11:12

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

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>
原创粉丝点击