简单教程ASP.NET获取CheckBoxList多选的值源代码
下面是ASP.NET程序中常用的获取CheckBoxList多选项值的例子,很简单,技术含量不高,对于初学者可以学习下,下面开始吧。 第1、前面ASPX页面源码: .ASPX Code [http://www.xueit.com] % @ Page Lan
下面是ASP.NET程序中常用的获取CheckBoxList多选项值的例子,很简单,技术含量不高,对于初学者可以学习下,下面开始吧。
第1、前面ASPX页面源码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SetGetCheckBoxList.aspx.cs" Inherits="SetGetCheckBoxList" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>如何设置与获取CheckBoxList多选的值。</title> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList ID="chkList" runat="server"> <asp:ListItem Value="0">zero</asp:ListItem> <asp:ListItem Value="1">one</asp:ListItem> <asp:ListItem Value="2">two</asp:ListItem> </asp:CheckBoxList> <asp:Button ID="btnSet" runat="server" Text="Set" OnClick="btnSet_Click" /> <asp:Button ID="btnGet" runat="server" Text="Get" OnClick="btnGet_Click" /> </div> </form> </body> </html>
第2、.cs后台源码

using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class SetGetCheckBoxList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSet_Click(object sender, EventArgs e) { foreach (ListItem li in chkList.Items) { if (li.Value == "0") { li.Selected = true; continue; } if (li.Value == "1") { li.Selected = true; continue; } } } protected void btnGet_Click(object sender, EventArgs e) { string chkSelect = ""; for (int i = 0; i < chkList.Items.Count; i ) { if (chkList.Items[i].Selected == true) chkSelect = chkList.Items[i].Value ","; } if (chkSelect != "") chkSelect = chkSelect.Substring(0, chkSelect.Length - 1); else chkSelect = "空"; Response.Write("<script>alert('选中的值为:" chkSelect "')</script>"); } }
就这样简单就可以实现了。
精彩图集
精彩文章
热门标签
Golang
列名
高消耗语句
忘记密码
UTFWry
读取文件内容
memory
基础小问题
链接服务器
js判断字符串
变量为空
随机干扰
排除某个元素
静音模式
缺省密码期限
病
发布项目
加载js文件
模仿登录
证书
捕获事件
RGB
移动div
ini文件操作类
XHTML
指定方法
SQL查询效率
web环境
复习
simple_html_
php旋转图片
apache
输入方式
php分页代码
layout
前端
动态分页
zend框架
传值传递
grep指令
parseJSON
ofstream
零学
string函数
填表
日期判断
imei
ACE_Task
自动提示
readfile
文字监控
加密类
struct.unpac
纵向表
web
hi
带附件
使用中文
header PHP 强
百度空间
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229