標籤:asp.net checkboxlist
.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判斷至少選擇一項的方法