总是不自动给我打勾,我重新测试了好多次,都不行。大家帮我看看啊。
<%@ 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;
}
}
}
} }
}
<%@ 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;
}
}
}
}
}
我就是用了具体的数据了
string str = "a1,a2,a3,a4,ds,ew,eee";
我还专门用lable来显示了items[3]里的数据了的。
数据库 应该也没有问题 (不然不会有数据绑定)
或许 与IsPostBack 有关 (猜的)
应该是数据库的问题,因为我改成另一个数据库表users的话,就可以自动打勾了。当然要有一些共同的数据。但是我看不出来这两个表有什么区别呀。真奇怪了。
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string strcom = "select n1 from rr";
SqlCommand sqlcom = new SqlCommand(strcom, conn);
SqlDataReader sdr1 = sqlcom.ExecuteReader();
List<rr> list=new List<rr>();
while(sdr1.read())
{
rr rr=new rr();
rr.n1=dr["n1"].ToString();
rr.n2=dr["n2"].ToString();
...
List.add(rr);
}
try
{
this.men.DataSource = list;
this.men.DataTextField = "n1";
this.men.DataValueField = "n1";
this.men.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sdr1.Close();
conn.Close();
conn.Dispose();
}
...数据类型 问题 nchar 与 nvarchar
感谢。原来真是数据类型的问题。
我开始创建表的时候,数据类型定的是默认的char,后来我直接修改成varchar,也不行,我就以为跟这个没关系了,后来就没往这方面想,但是,刚才你提示了一下,我就想,是不是不能直接修改?然后我就把这个表删除了,重新建一个,把类型定为varchar,哎,就对了!这个问题折磨了我两天了!我哭。不过,话又说回来,为什么不能直接修改呢?当然,我在修改的时候,提示了这个的:我没当回事,谁能告诉我一下,为什么这样改不行。谢谢。
你把断点打在这 if li.Text == items[i])看看这个条件成立不成立不成立在做响应修改。
嗯,感谢关注,你说的也对,其实一直以来这个都是不成立的。
刚才复习了一下char、varchar的概念,才知道我犯了一个极其低极的错误。
在我建表的时候,使用的是nchar(8)类型,由于我输入的字符不够,系统就自动帮我填充了一些空格,当然就跟其它实际数据对不上了,然后我再在数据库里改成varchar(8)类型,又犯了一个错误,因为空格还是存在的,只是看不见而已,当然,系统曾经提示过我。我一直在转弯弯,呵呵,感谢大家。