还是关于CheckBoxList。

gootey gootey 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
总是不自动给我打勾,我重新测试了好多次,都不行。大家帮我看看啊。


<%@ 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;
}
}
}
}

}
}





...全文
评论 21
收藏 已收藏
分享
返回顶部
gootey
.NET社区 62,275 成员 668,967 内容