[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2637
  • Last Modified:

Checkbox list item selected property is not working!


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?
  • 2
  • 2
1 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
Jules211Author Commented:
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?
In page_load, you only build the check box list the first time, that is when the request is not a postback (Not IsPostback).
Jules211Author Commented:
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!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now