无法从数据库查看 CheckBoxList

发布于 2024-12-21 23:48:11 字数 2011 浏览 7 评论 0原文

我有 2 个页面,一个用于将数据添加到数据库,另一个用于编辑它,因此在添加中我有一个 CheckBoxList ,我将它们添加到数据库

URLS 中,如下所示:1,2,3,4,5 数字是复选框中的值“键”,

我使用了此代码

String values = "";        
foreach (ListItem i in CheckBoxList1.Items)
{
      if (CheckBoxList1.Items.Count == 1)
             values += i.Value;
      else
        if (i.Selected)
        {
             values += i.Value + ",";

        }
}

,然后将这些值添加到数据库中,效果非常好, 现在我的问题是在编辑页面中,因为首先我想显示数据库中的复选框,我使用了这个,但它

在页面中不起作用加载

protected void Page_Load(object sender, EventArgs e)
{

        if (!Page.IsPostBack)
        {
          ... connection to the database .. etc

            try
            {
                con.Open();
                rd = cmd.ExecuteReader();
                if (rd.Read())
                {                 
                    String values = rd["urls"].ToString();//workes perfect

                    string[] arr = values.Split(',');//works perfect
                    int x = CheckBoxList1.Items.Count;//this will get me a zero

                    foreach (ListItem item in CheckBoxList1.Items)// doesnt enter here
                    {
                        foreach (string s in arr)
                        {
                            if (item.Text == s)
                            {
                                item.Selected = true;
                            }
                        }
                    }

             .... //exceptions handling

我的aspx页面代码

 <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="urls_ds" 
                        DataTextField="name" DataValueField="id">
   </asp:CheckBoxList>
  <asp:SqlDataSource ID="urls_ds" runat="server" 
      ConnectionString="<%$ ConnectionStrings:testing_cs %>" 
       SelectCommand="SELECT * FROM [tbl_urls]"></asp:SqlDataSource>

我使用SqlDataSource从数据库获取复选框列表,在页面中,当我打印项目计数为零时,我可以看到它们放在哪里,这

可能是问题所在?

谢谢

I have 2 pages , one to add data to database , and the other to edit it, so in the add I have a CheckBoxList , I added them as follow in the database

URLS : 1,2,3,4,5
and the numbers are the values "keys" from the checkbox

I used this code

String values = "";        
foreach (ListItem i in CheckBoxList1.Items)
{
      if (CheckBoxList1.Items.Count == 1)
             values += i.Value;
      else
        if (i.Selected)
        {
             values += i.Value + ",";

        }
}

and then I added the values to the database , and it worked perfect ,
now my problem is in the edit page ,as first I want to show the checked boxes from the database , I used this but its not working

in the page load

protected void Page_Load(object sender, EventArgs e)
{

        if (!Page.IsPostBack)
        {
          ... connection to the database .. etc

            try
            {
                con.Open();
                rd = cmd.ExecuteReader();
                if (rd.Read())
                {                 
                    String values = rd["urls"].ToString();//workes perfect

                    string[] arr = values.Split(',');//works perfect
                    int x = CheckBoxList1.Items.Count;//this will get me a zero

                    foreach (ListItem item in CheckBoxList1.Items)// doesnt enter here
                    {
                        foreach (string s in arr)
                        {
                            if (item.Text == s)
                            {
                                item.Selected = true;
                            }
                        }
                    }

             .... //exceptions handling

my code for the aspx page

 <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="urls_ds" 
                        DataTextField="name" DataValueField="id">
   </asp:CheckBoxList>
  <asp:SqlDataSource ID="urls_ds" runat="server" 
      ConnectionString="<%$ ConnectionStrings:testing_cs %>" 
       SelectCommand="SELECT * FROM [tbl_urls]"></asp:SqlDataSource>

I am getting the checkboxlist from the database using SqlDataSource , and in the page I can see them put as I print the count of the items its zero

where could be the problem ??

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-12-28 23:48:11

编辑:这篇文章准确地描述了您的问题 http://forums.asp.net/t/1488957 .aspx/1,并提供了两种解决方案

这是我所看到的

if(!Page.IsPostBack)

这意味着您位于页面的新副本上,因此 CheckBoxList1 是空的,直到您加载数据为止。我敢打赌这会发生在你的代码中。只需确保在使用之前加载它即可。

编辑:当使用 SqlDataSource 填充控件时,您必须记住引用它的控件在 Page_Load 执行后绑定。上面的链接提供了两种解决方法(手动调用 Control.DataBind 或处理 Control.Databound 事件)。

Edit : This post describes your problem exactly http://forums.asp.net/t/1488957.aspx/1, and offers two solutions

Here is what I see

if(!Page.IsPostBack)

This means you are on a NEW copy of the page, so CheckBoxList1 is empty until you load it with data. which I bet happens further down in your code. Just make sure you load it before you use it.

Edit : When using a SqlDataSource to populate controls, you must remember that controls referencing it bind AFTER Page_Load execution. The link above gives two work around methods (either manually calling Control.DataBind or handling the Control.Databound event).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文