ASP.NET单选框与多选框值获取

【多选获取值】:

<div>

<asp:Label ID="Label1" runat="server" Text="ListBox多选"></asp:Label><br />

<asp:ListBox ID="ListBox1" runat="server" Height="103px" Width="212px" SelectionMode="Multiple">

<asp:ListItem>Java</asp:ListItem>

<asp:ListItem>HTML</asp:ListItem>

<asp:ListItem Value="Web">Web前端开发</asp:ListItem>

</asp:ListBox>

<br />

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

</div>


public partial class Default2 : System.Web.UI.Page

{

String i;//1.定义变量

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

foreach(ListItem lst in ListBox1.Items)//2.循环取出(ListBox1为多选控件的ID)

{

if (lst.Selected)

{

i += lst.Text;

}

}

Response.Write(i);//在网页上输出i变量的内容

}

}


【单选获取值】:

string gender = rblxb.SelectedValue;//将列表控件id为rblxb的Value值赋给字符串变量gender


protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)

{

String jg = RadioButtonList1.SelectedValue.ToString();

if (jg == "wqndyd")

{

s1.Text = "测试";

}

}

相关推荐
geovindu42 分钟前
CSharp: Observer Pattern
开发语言·后端·观察者模式·设计模式·c#·.netcore·行为模式
Flynt1 小时前
把公司项目迁到 Spring Boot 4.0:编译通过只是开始
java·spring boot·后端
IT_陈寒1 小时前
Redis误用keys命令把生产环境搞崩了,血的教训
前端·人工智能·后端
她的男孩2 小时前
多租户和数据权限怎么共存?扒完拦截器注册链路,我找到 4 个隐蔽的坑
java·后端·架构
用户8356290780512 小时前
使用 Python 设置 Excel 页眉和页脚
后端·python
步行cgn2 小时前
IoC 控制反转:从概念到 Spring 的实现
后端
爱勇宝2 小时前
没有 Fn 键关触控板?我做了一个双击即用的 Windows 小工具
前端·后端·程序员
沙湖遇雨3 小时前
8.ByteBuf 的数据模型、生命周期和内存分配
后端
by组态3 小时前
Ricon组态系统API参考手册
前端·后端·物联网