今天看到一个新控件,名为RadioButtonList,其实就是单选框的一个列表。由于之前没有接触过,所以第一次总会让人傻眼,于是乎上网查了一下相关资料说明,就出了今天的例子。
void Button1_Click(object Source, EventArgs e)
{
//此处是判断RadioButtonList里面有没有Item项
//因为索引都是从0开始的,有的话不选择所选项的值
if (RadioButtonList1.SelectedIndex > -1)
{
Label1.Text = “You selected: ” + RadioButtonList1.SelectedItem.Text;
}
}
void chkLayout_CheckedChanged(Object sender, EventArgs e)
{
//规定 CheckBoxList 中项目的布局。
//可能的值:
//Flow – 项目不显示在表格中
//Table – 默认。项目显示在表格中
if (chkLayout.Checked == true)
{
RadioButtonList1.RepeatLayout = RepeatLayout.Table;
}
else
{
RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
}
}
void chkDirection_CheckedChanged(Object sender, EventArgs e)
{
//规定 RadioButtonList 中项目的布局方向。
//可能的值:
//Horizontal – 项目水平显示
//Vertical – 默认。项目垂直显示。
if (chkDirection.Checked == true)
{
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
}
else
{
RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
}
}
</script>
</head>
<body>
<h3>RadioButtonList Example</h3>
<form runat=server>
<asp:RadioButtonList id=RadioButtonList1 runat=”server”>
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
<p>
<asp:CheckBox id=chkLayout OnCheckedChanged=”chkLayout_CheckedChanged” Text=”Display Table Layout” Checked=true AutoPostBack=”true” runat=”server” />
<br>
<asp:CheckBox id=chkDirection OnCheckedChanged=”chkDirection_CheckedChanged” Text=”Display Horizontally” AutoPostBack=”true” runat=”server” />
<p>
<asp:Button id=Button1 Text=”Submit” οnclick=”Button1_Click” runat=”server”/>
<p>
<asp:Label id=Label1 font-name=”Verdana” font-size=”8pt” runat=”server”/>
</form>
</body>
</html>
当然拉,这个例子呢,不是我的,是中文版MSDN里面的例子,我呢,只是在里面做下注释而已,不过我有点不明白为什么里面的例子都不用隐藏技术的,难道是因为这样好显示一些吗?不知道了。
不过我在项目里需要的是如下的代码,权当以后参照。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37035.html