求救 asp.net CheckBoxList从数据库读取数据设定是否选中,急急急 20
页面<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Defa...
页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="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 = "cblJL" runat = "server" RepeatDirection="Horizontal" Width="56%" DataSourceID="SqlDataSource1" DataValueField="test_id" DataTextField="test_name">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [test]"></asp:SqlDataSource>
<asp:CheckBoxList ID = "CheckBoxList1" runat = "server" RepeatDirection="Horizontal" Width="55%">
<asp:ListItem>当前记录</asp:ListItem>
<asp:ListItem>当前页</asp:ListItem>
<asp:ListItem>全部记录</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
后台
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string name = "当前记录,当前页,";
string[] names;
names = name.Split(',');
foreach (string str in names)
{
for (int i = 0; i < cblJL.Items.Count; i++)
{
if (this.cblJL.Items[i].Value == str)
{
this.cblJL.Items[i].Selected = true;
}
}
}
foreach (string str in names)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Value == str)
{
this.CheckBoxList1.Items[i].Selected = true;
}
}
}
}
}
}
为什么第一个从数据库中绑定数据的无法设定为是否选中啊!!! 展开
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="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 = "cblJL" runat = "server" RepeatDirection="Horizontal" Width="56%" DataSourceID="SqlDataSource1" DataValueField="test_id" DataTextField="test_name">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [test]"></asp:SqlDataSource>
<asp:CheckBoxList ID = "CheckBoxList1" runat = "server" RepeatDirection="Horizontal" Width="55%">
<asp:ListItem>当前记录</asp:ListItem>
<asp:ListItem>当前页</asp:ListItem>
<asp:ListItem>全部记录</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
后台
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string name = "当前记录,当前页,";
string[] names;
names = name.Split(',');
foreach (string str in names)
{
for (int i = 0; i < cblJL.Items.Count; i++)
{
if (this.cblJL.Items[i].Value == str)
{
this.cblJL.Items[i].Selected = true;
}
}
}
foreach (string str in names)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Value == str)
{
this.CheckBoxList1.Items[i].Selected = true;
}
}
}
}
}
}
为什么第一个从数据库中绑定数据的无法设定为是否选中啊!!! 展开
4个回答
展开全部
你是从数据库读取,那么你数据库里面这个字段应该为0或1,你在后台写,如果为0,那么这个CheckBox就为false,否则为true就行了啊,本来就是bool值,你读取出来就这两个结果,判断下很简单的嘛
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void Page_Load(object sender, EventArgs e)
{
string name = "当前记录,当前页,";
string[] strtemp = name.Split(',');
foreach (string str in strtemp)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Value == str)
{
this.CheckBoxList1.Items[i].Selected = true;
}
}
}
}
试试看...
{
string name = "当前记录,当前页,";
string[] strtemp = name.Split(',');
foreach (string str in strtemp)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Value == str)
{
this.CheckBoxList1.Items[i].Selected = true;
}
}
}
}
试试看...
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.CheckBoxList1.Items[i].Selected = true;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该不能这样设置,,
要也是设置selectindex...
要也是设置selectindex...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询