Checkbox list item selected property is not working!

Posted on 2006-05-17
Last Modified: 2008-01-09

I am trying to get the selected value of a checkbox list.  At run time I add the values to the list like this:

MyChkList.Items.Add(New ListItem(strName, strName))

When I check the first listbox and click a button to fire off code to retrieve the selected value, it is not recognizing the item I selected:

For i = 0 To MyChkList.Items.Count - 1
       If MyChkList.Items(i).Selected Then
           Response.Write(MyChkList.Items(i).Text + "<br />")
       End If

The "if" clause does not evaluate to true.  Why?
Question by:Jules211
    LVL 12

    Accepted Solution

    Did you rebuild the checkbox list in Page_Load?
    If that's the case, your selection get lost after Page_Load but before you button click event handler.

    You can do this instead:
    If Not IsPostback Then
     build the checkbox list
    End if

    Author Comment

    Yes, I did build it in page load.
    So I need to rebuild in the button click event?  Or are you saying to do that in page load?
    LVL 12

    Expert Comment

    In page_load, you only build the check box list the first time, that is when the request is not a postback (Not IsPostback).

    Author Comment

    I really should have known that- lol.  I have it coded that way in some of my other sites but it's been a while since I wrote a website.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now