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 = "测试";

}

}

相关推荐
前端工作日常8 小时前
我学习到的Java 的 Service 分层:itf 和 impl 到底是什么?
java·后端
前端工作日常9 小时前
我学习到的JIT即时编译与机器码缓存失效
java·后端
犀利豆9 小时前
Claude Code Tools 研究系列(三)ExitPlanMode
人工智能·后端
星栈9 小时前
oh-my-pi工程级AI编码工使用体验
人工智能·后端·agent
立心者011 小时前
SpringBoot中使用TOTP实现MFA(多因素认证)
java·spring boot·后端
AskHarries12 小时前
SEO 页面怎么生成
后端
爱吃牛肉的大老虎12 小时前
Rust对象之结构体,枚举,特性
开发语言·后端·rust
石榴12 小时前
NestJS 的请求到底经过了什么:装饰器、守卫、拦截器、管道与中间件如何配合
后端
Gopher_HBo12 小时前
moby-client客户端
后端
杨运交13 小时前
[055][调度模块]Spring动态任务调度框架的设计与实现
java·后端·spring