.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1_CheckBoxList.aspx.cs" Inherits="_1_CheckBoxList" %>
<!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></title>
<script src="JS/jquery-1.8.3.js" type="text/javascript"></script>
<style type="text/css">
body,div,h2{ margin:0px; padding:0px;}
.warn { font-size:12px; color:Red;}
</style>
<script type="text/javascript">
function EmptyCheck(_chk, _span, _txt) {
var _check = $(":checked", _chk).val();
if (_check==null) {
$(_span).text("请至少选择1项!");
$(_txt).val("");
return false;
} else {
$(_span).text("");
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>CheckBoxList控件应用实例:</h2>
<p>1.手动设置项: <span class="warn" id="err_chk_1"></span></p>
<%--注意checkBoxList的最终呈现:是在一个TABLE之内--%>
<asp:CheckBoxList ID="chkList_1" runat="server">
<asp:ListItem Value="A">第一项</asp:ListItem>
<asp:ListItem Value="B">第二项</asp:ListItem>
<asp:ListItem Value="C">第三项</asp:ListItem>
<asp:ListItem Value="D">第四项</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="btn_1" runat="server" Text="检测哪几项被选中"
OnCommand="checkResult" CommandArgument="chkList_1,txt_result_1"
OnClientClick="return EmptyCheck(‘#chkList_1‘,‘#err_chk_1‘,‘#txt_result_1‘)"
/>
<asp:TextBox ID="txt_result_1" runat="server" Width="400"></asp:TextBox>
<hr />
<p>2. 从数据库中获取列表<span class="warn" id="err_chk_2"></span></p>
<asp:CheckBoxList ID="chkList_2" runat="server" DataSourceID="ADS_CHK2"
DataTextField="se_name" DataValueField="se_id">
</asp:CheckBoxList>
<asp:AccessDataSource ID="ADS_CHK2" runat="server" DataFile="~/DATA/EV.mdb"
SelectCommand="SELECT [se_name], [se_id] FROM [T_SERIES]">
</asp:AccessDataSource>
<asp:Button ID="btn_2" runat="server" Text="检测CHK2哪几项被选中"
OnCommand="checkResult" CommandArgument="chkList_2,txt_result_2"
CommandName="cmd_selist"
OnClientClick="return EmptyCheck(‘#chkList_2‘,‘#err_chk_2‘,‘#txt_result_2‘)"
/>
<asp:TextBox ID="txt_result_2" runat="server" Width="400"></asp:TextBox>
<p>
构建SQL语句:<br />
<asp:TextBox ID="txt_sql" runat="server" TextMode="MultiLine" Rows="4" Width="400" />
</p>
</div>
</form>
</body>
</html>
设计视图如下:
.aspx.cs代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _1_CheckBoxList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //自定义函数: 构建listitem的TEXT和VALUE组成的字符串 protected string getResult(ListItem _li) { string _result = ""; _result = _li.Text + ":" + _li.Value + ","; return _result; } //onCommand代码,根据e携带的参数,确定对哪一组checkboxlist处理, //并将结果输出到那个文本框 //##### ! 特别注意:文本框,checkBoxList元素的ID都是带相同后缀的构成方式, // 思考他们出现在ListView下,clientIDMode设置成Predictable后,参数e该如何设置 protected void checkResult(object s, CommandEventArgs e) { //分割参数,获取两个ID string[] id = e.CommandArgument.ToString().Split(','); //由参数获取对应的checkListBox和TextBox CheckBoxList _chkList = (CheckBoxList)FindControl(id[0]); TextBox _txt = (TextBox)FindControl(id[1]); //清空textbox结果; _txt.Text = ""; string _in = ""; //遍历获取结果 foreach (ListItem li in _chkList.Items) { if (li.Selected) { _txt.Text += getResult(li); //由自定义函数完成字符串构建 _in += li.Value + ","; //构建SQL语句需要的部分字符串 } } //对不同commandName进行分支处理 switch (e.CommandName) { case "cmd_selist": string sql_s = "SELECT * FROM TB_MONITOR "; string sql_w = "WHERE mo_seid in (" + _in + ")"; //SQL语句,列表中,最后多一个",",不会影响执行结果 txt_sql.Text = sql_s + '\n' + sql_w; break; } } }
asp.net中checkboxlist判断至少选择一项的方法
原文地址:http://blog.csdn.net/yayun0516/article/details/41842231