后台选中 radio

DoubleMars 2009-03-16 11:08:16
在asp.net中的一个webform中
有一个radio控件
<input type=radio name=rd value="男">男
<input type=radio name=rd value="男">女


在对应的后台文件中
从数据库中得到一个string型变量sex

如何让实现让rd的所有值和sex比较,假如相等,则对应的radio被选中


请问如何实现啊谢谢
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bsetsail 2009-03-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 DoubleMars 的回复:]
不用 runat=server 怎么实现??
[/Quote]

后臺定義public變量 public string s = "";
用腳本實現
<script language="javascript">
if("<%=s%>" == "男")
form1.rd1.checked = true;
else
form1.rd2.checked = true;
</script>
DoubleMars 2009-03-16
  • 打赏
  • 举报
回复
不用 runat=server 怎么实现??
pgameli 2009-03-16
  • 打赏
  • 举报
回复
页面文件给分一下组
<asp:RadioButton ID="rbt1" runat="server" AutoPostBack="True" Checked="True"
Font-Size="Small" GroupName="sex" Text="男"/>
<asp:RadioButton ID="rbt2" runat="server" AutoPostBack="True" Font-Size="Small" GroupName="sex" Text="女" />

后台得到string的时候这样写判断
string sex="这里是得到的变量";
if(sex.equal("男")){
rbt1.Checked=true;
}
else
{
rbt2.Checked=true;
}
CN_Azure 2009-03-16
  • 打赏
  • 举报
回复
修正

string sex = "";
//从数据库中获取性别 比如女
if(sex == "女")
{
RadioButton2.Checked = Ture;
}else
{
RadioButton1.Checked = Ture;
}

CN_Azure 2009-03-16
  • 打赏
  • 举报
回复

string sex = "";
//从数据库中获取性别 比如女
if(sex == "女")
{
RadioButton2.Checked;
}else
{
RadioButton1.Checked;
}
liuyeede 2009-03-16
  • 打赏
  • 举报
回复
用后太代码就不要用<input type=radio name=rd value="男">男
<input type=radio name=rd value="男">女 这中Html标记了,一楼正解
bsetsail 2009-03-16
  • 打赏
  • 举报
回复
<input type=radio id="rd1" runat="server" name=rd value="男">男
<input type=radio id="rd2" runat="server" name=rd value="男">女


if(sex=="男")
this.rd1.Checked = true;
else
this.rd2.Checked = true;
CN_Azure 2009-03-16
  • 打赏
  • 举报
回复
页面:

<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" Checked="True"
Font-Size="Small" GroupName="selectView" Text="男" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" Font-Size="Small"
GroupName="selectView" Text="女"
oncheckedchanged="RadioButton2_CheckedChanged" />


if(RadioButton1.Checked){
//code选中男
}else if(RadioButton2.Checked)
{
//code选中女
}


关键GroupName,两个RadioButton的GroupName要一样

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧