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

}

}

相关推荐
葫芦和十三1 天前
图解 MongoDB 13|WiredTiger 存储引擎:B-tree、页和 checkpoint 三件套
后端·mongodb·agent
葫芦和十三1 天前
图解 MongoDB 14|Cache 与淘汰:WiredTiger 的内存治理
后端·mongodb·面试
IT_陈寒1 天前
Vue这个坑我跳了两次,原来问题出在这
前端·人工智能·后端
ServBay1 天前
9 个 Python 第三方库推荐,不用 AI 都好像多出一个团队
后端·python
用户8356290780511 天前
如何使用 Python 添加和管理 Excel 批注(完整示例)
后端·python
用户8356290780511 天前
使用 Python 管理 Excel 工作表:创建、复制、删除与重命名
后端·python
lizhongxuan1 天前
Agent Tool
后端
CaffeinePro1 天前
依赖注入:FastAPI最核心的解耦能力案例解析
后端·fastapi
Assby1 天前
从 Function Calling 到 MCP:理解 Agent 工具调用的底层通信机制
人工智能·后端
打字机v1 天前
创建第一个spring-boot项目
后端