关于checkboxlist自动打勾

gootey 2011-03-15 10:17:28


protected void Page_Load(object sender, EventArgs e)
{
string str = "go1,go2,go3,go4";
string[] items = str.Split(',');
for (int i = 0; i < items.Length; i++)
{
if (CheckBoxList2.Items.FindByText(items[i]) != null)
{
CheckBoxList2.Items.FindByText(items[i]).Selected = true;
}
}
}



这是对静态的,如果我要绑定数据库,从数据库取出数据,有的话就打勾,没有的话就不打勾,该怎么实现呢?
...全文
302 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2011-03-16
  • 打赏
  • 举报
回复
应该是执行顺序导致你的问题
孟子E章 2011-03-16
  • 打赏
  • 举报
回复
不可能吧。
1,你需要先绑定数据库,然后再执行打勾操作
2,确认没有空格等不可见字符影响
gootey 2011-03-16
  • 打赏
  • 举报
回复
我是要从数据库里取出数据再跟一个固定的表进行对比,如果数据库里存在某一项,就自动打勾。
现在不管怎么弄,就是不给我打勾。。
wshqszw1 2011-03-16
  • 打赏
  • 举报
回复
直接循环一个checkbox不行么?
gootey 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 tao145 的回复:]
引用 11 楼 gootey 的回复:
引用 10 楼 wxr0323 的回复:
到底是CheckBoxList1还是CheckBoxList2

for (int i = 0;i<CheckBoxList1.Items Counti++ )
{
if(ChkList.Items[i]==li.Text)
{
ChkList.Items[i].checked;
……
[/Quote]



protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string str = "go1,go2,go3,go4";
string[] items = str.Split(',');
foreach (ListItem li in this.CheckBoxList1.Items)
{
for (int i = 0; i < items.Length; i++)
{
if (li.Value == items[i])
{
li.Selected = true;
}
}
}
}

}



啊,我是这样用过,怎么没有作用的?
笑道江湖情 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gootey 的回复:]
引用 10 楼 wxr0323 的回复:
到底是CheckBoxList1还是CheckBoxList2

for (int i = 0;i<CheckBoxList1.Items Counti++ )
{
if(ChkList.Items[i]==li.Text)
{
ChkList.Items[i].checked;
}
}

对你的关注表示感谢,……
[/Quote]

根据item的value


<!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>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>





public partial class SelectItemByValue : System.Web.UI.Page
{
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;
}
}
}
}
}
}
gootey 2011-03-16
  • 打赏
  • 举报
回复

解决了。其实4楼就是答案,我一直忽略了,哈哈。

我把前台数据绑定取消,改到后台来就行了!

为什么那么多的人没有发现这个问题呢?呵呵?

为什么我在前台绑定就不行呢?

哪位大侠解释一下呢。



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 ceshi : 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 user_name from users";
SqlCommand sqlcom = new SqlCommand(strcom, conn);
SqlDataReader sdr = sqlcom.ExecuteReader();
try
{
this.CheckBoxList1.DataSource = sdr;
this.CheckBoxList1.DataTextField = "user_name";
this.CheckBoxList1.DataValueField = "user_name";
this.CheckBoxList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sdr.Close();
conn.Close();
conn.Dispose();
}
string str = "gootey,zf,go3,go4";
string[] items = str.Split(',');
foreach (ListItem li in this.CheckBoxList1.Items)
{
for (int i = 0; i < items.Length; i++)
{
if (li.Text == items[i])
{
li.Selected = true;
}
}
}
}

}


}


terryida 2011-03-16
  • 打赏
  • 举报
回复

string str = "gootey,zf,go3,go4";
string[] items = str.Split(',');
for (int j = 0; j < CheckBoxList1.Items.Count; j++)
{
for (int i = 0; i < items.Length; i++)
{
if (CheckBoxList1.Items[j].Value.ToString().Trim() == items[i].ToString().Trim() )
{
CheckBoxList1.Items[j].Selected = true; }
}
}

gootey 2011-03-16
  • 打赏
  • 举报
回复
帮我看一下具体是哪出问题了。谢谢了啊。



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ceshi.aspx.cs" Inherits="ceshi" %>
<%@ Import Namespace="System.Data" %>

<!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">
<script language="javascript" src ="calendar.js" type="text/javascript" charset="gb2312"></script>
<style type="text/css">
a{TEXT-DECORATION:none}
A:link {COLOR:Black;}
A:visited{COLOR:Black;}
A:hover {COLOR: red;}
</style>
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="user_name"
DataValueField="user_name" DataMember="DefaultView" EnableTheming="True"
Height="16px"
Width="246px" BorderStyle="None" RepeatLayout="Flow">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WHConn %>"
SelectCommand="SELECT [user_name] FROM [users]"></asp:SqlDataSource>

</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 ceshi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string str = "gootey,zf,go3,go4";
string[] items = str.Split(',');
foreach (ListItem li in this.CheckBoxList1.Items)
{
for (int i = 0; i < items.Length; i++)
{
if (li.Text == items[i])
{
li.Selected = true;
}
}
}
}

}


}


wshqszw1 2011-03-16
  • 打赏
  • 举报
回复
                    string[] sort = cd.Remark.Split(',');
foreach (string item in sort)
{
for (int i = 0; i < repeater1.Controls.Count; i++)
{
CheckBox box = repeater1.Controls[i].FindControl("cb_Sort") as CheckBox;
if (item == box.Text)
{
box.Checked = true;
}
}
}

我这是循环的checkbox参考吧
gootey 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wxr0323 的回复:]
到底是CheckBoxList1还是CheckBoxList2

for (int i = 0;i<CheckBoxList1.Items Counti++ )
{
if(ChkList.Items[i]==li.Text)
{
ChkList.Items[i].checked;
}
}
[/Quote]
对你的关注表示感谢,CheckBoxList1或者CheckBoxList2都是一样的,呵呵,我忘了改了,不过在foreach里不是已经循环过CheckBoxList了么?为什么在for里还要循环,另外,我的思路表面上看起来没有错呀,为什么没有效果呢?
子夜__ 2011-03-15
  • 打赏
  • 举报
回复
到底是CheckBoxList1还是CheckBoxList2

for (int i = 0;i<CheckBoxList1.Items Counti++ )
{
if(ChkList.Items[i]==li.Text)
{
ChkList.Items[i].checked;
}
}
子夜__ 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gootey 的回复:]

快来人帮忙看一下呀。。我改成这样也不行:
C# code


protected void Page_Load(object sender, EventArgs e)
{
string str = "go1,go2,go3,go4";
string[] items = str.Split(',');
foreach (ListI……
[/Quote]
你干啥还要在加一层循环?
gootey 2011-03-15
  • 打赏
  • 举报
回复
快来人帮忙看一下呀。。我改成这样也不行:


protected void Page_Load(object sender, EventArgs e)
{
string str = "go1,go2,go3,go4";
string[] items = str.Split(',');
foreach (ListItem li in this.CheckBoxList1.Items)
{

for (int i = 0; i < items.Length; i++)
{
if (li.Text==items[i])
{
li.Selected = true;
}
}
}

}

gootey 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxr0323 的回复:]
C# code
if(!this.IsPostBack)
{
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
con.Open();
SqlComma……
[/Quote]

哥,有什么办法能获得CheckBoxList2子项里的数据么?比如Item.text什么的
gootey 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sxldfang 的回复:]
你是说下面的字符串从数据库中获取吗?

string str = "go1,go2,go3,go4";
[/Quote]

没有,我是说CheckBoxList2里的数据从数据库里获得,str还是静态的。
sxldfang 2011-03-15
  • 打赏
  • 举报
回复
你是说下面的字符串从数据库中获取吗?

string str = "go1,go2,go3,go4";
子夜__ 2011-03-15
  • 打赏
  • 举报
回复
if(!this.IsPostBack)
{
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
con.Open();
SqlCommand cmd=new SqlCommand("select top 10 * from Employees",con);
SqlDataReader sdr=cmd.ExecuteReader();
try
{
this.chklistA.DataSource=sdr;//数据源
this.chklistA.DataTextField="";//显示的字段
this.chklistA.DataValueField="";//值的字段
this.chklistA.DataBind();//绑定
}
catch(Exception Ex)
{
throw Ex;
}
finally
{
sdr.Close();
con.Close();
con.Dispose();
}
}


foreach(ListItem li in this.checkboxlist2.Items)
{
if(ListItem.Text==checkboxlist2.SelectedValue)//如果不行换ListItem.Value
{
li.Selectedrue
}
}

没有测试。自己在改改
gootey 2011-03-15
  • 打赏
  • 举报
回复
额,可能是我表达得不够清楚,我是说让checkboxlist2去绑定一个数据源。
子夜__ 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 gootey 的回复:]
C# code


protected void Page_Load(object sender, EventArgs e)
{
string str = "go1,go2,go3,go4";
string[] items = str.Split(',');
for (int i = 0; i < items.Length; i++)
……
[/Quote]

静态能实现 动态就不能实现了啊、?



string str = "go1,go2,go3,go4";
把这个后面的结果 替换成从数据库中查出来不就行了。?
加载更多回复(1)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧