A界面代码:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
</head>
<script type="text/javascript">
function limit(cbl) {
var cbs = document.getElementByIdx("<%= CheckBoxList1.ClientID %>").getElementsByTagName_r("input");
var count = 0;
for (var i = 0; i < cbs.length; i++) {
if (cbs[i].type == "checkbox") {
if (cbs[i].checked) {
count++;
}
}
}
if (count > 3) {
cbl.checked = false;
alert("不能多于3项");
return;
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" ForeColor="White" CellPadding="0"
CellSpacing="0" Height="70px">
<asp:ListItem Value="0"> Weekly email newsletter</asp:ListItem>
<asp:ListItem Value="1"> Sale and special offer updates</asp:ListItem>
<asp:ListItem Value="2"> Do not send me any more emails</asp:ListItem>
<asp:ListItem Value="3"> Do not send me any more emails</asp:ListItem>
<asp:ListItem Value="4"> Do not send me any more emails</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
B界面代码:
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (ListItem li in CheckBoxList1.Items)
li.Attributes.Add("onclick", "limit(this)");
}
protected void Button1_Click(object sender, EventArgs e)
{
string EmailSendType = null;
int n = this.CheckBoxList1.Items.Count;
for (int i = 0; i < n; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
if (EmailSendType != null)
EmailSendType += "," + CheckBoxList1.Items[i].Value;
else
EmailSendType = CheckBoxList1.Items[i].Value;
}
}
string[] str = EmailSendType.Split(",");//此处里面为单引号
foreach (string s in str)
{
if (s.Trim() != "")
{
if (s == "0")
{
Response.Write("aa0");
}
if (s == "1")
{
Response.Write("bb1");
}
if (s == "2")
{
Response.Write("cc2");
}
if (s == "3")
{
Response.Write("dd3");
}
}
Response.Write(s+"<br>");
}
}
}