Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6948865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:52:43+00:00 2026-05-27T13:52:43+00:00

I have 2 pages , one to add data to database , and the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:52:44+00:00Added an answer on May 27, 2026 at 1:52 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution with 3 projects.One of UI (contains web pages) and one
I have some ASP.NET Master Pages located in one assembly. I need to use
We have some web pages which have two frames, with one frame in the
I have one master page which applies to all pages in this website. On
I have the following snippet in one of my html pages : <div class=inputboximage>
I have a Dojo Datagrid in one of my pages (which contains more content)
I have a page that has an iframe From one of the pages within
I have a POST request coming to one of my pages, here is a
I have a number of pages in my site, as one would expect. For
I have a domain with a loto of indexed pages, I use this one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.