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

}

}

相关推荐
nbwenren9 小时前
Springboot中SLF4J详解
java·spring boot·后端
helx829 小时前
SpringBoot中自定义Starter
java·spring boot·后端
rleS IONS10 小时前
SpringBoot获取bean的几种方式
java·spring boot·后端
lifewange10 小时前
Go语言-开源编程语言
开发语言·后端·golang
白毛大侠11 小时前
深入理解 Go:用户态和内核态
开发语言·后端·golang
王码码203511 小时前
Go语言中的数据库操作:从sqlx到ORM
后端·golang·go·接口
星辰_mya12 小时前
雪花算法和时区的关系
数据库·后端·面试·架构师
计算机学姐13 小时前
基于SpringBoot的兴趣家教平台系统
java·spring boot·后端·spring·信息可视化·tomcat·intellij-idea
總鑽風13 小时前
单点登录springcloud+mysql
后端·spring·spring cloud
0xDevNull13 小时前
Java 11 新特性概览与实战教程
java·开发语言·后端