62,244
社区成员




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="men" runat="server" AutoPostBack="True"
RepeatDirection="Horizontal" RepeatLayout="Flow">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strcon = ConfigurationManager.ConnectionStrings["WHConn"].ConnectionString;
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string strcom = "select n1 from rr";
SqlCommand sqlcom = new SqlCommand(strcom, conn);
SqlDataReader sdr1 = sqlcom.ExecuteReader();
try
{
this.men.DataSource = sdr1;
this.men.DataTextField = "n1";
this.men.DataValueField = "n1";
this.men.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sdr1.Close();
conn.Close();
conn.Dispose();
}
string str = "a1,a2,a3,a4,ds,ew,eee";
string[] items = str.Split(',');
foreach (ListItem li in this.men.Items)
{
for (int i = 0; i < items.Length; i++)
{
if (li.Text == items[i])
{
li.Selected = true;
}
}
}
}
}
}
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1" Text="CheckBox1"></asp:ListItem>
<asp:ListItem Value="2" Text="CheckBox2"></asp:ListItem>
<asp:ListItem Value="3" Text="CheckBox3"></asp:ListItem>
<asp:ListItem Value="4" Text="CheckBox4"></asp:ListItem>
<asp:ListItem Value="5" Text="CheckBox5"></asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string[] str = new string[] { "1", "3", "5" };
foreach (ListItem item in this.CheckBoxList1.Items)
{
for (int i = 0; i < str.Length; i++)
{
if (item.Value == str[i])
{
item.Selected = true;
}
}
}
}
}