471,350 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Losing selectedIndex on Control inherted from ListBox during postback

I have created a Web Custom Control that inherits from
WebControls.ListBox but can't get it to retain the selectedIndex after
a postback. As a test I created a very simple version of the control
with no additional code. I just inherited from the exisiting ListBox
class and put the control on a page.
I put this control a blank page along with a button to perform a post
back. I select an item in the list box and then click the button and
after the postback I have lost the selected value. I added some
javascript to alert to the selectedIndex just to make sure the client
had one and it did.
EnableViewState is set to true for this control. If I make a custom
textbox in the same method it's value is retained after postback with
no additional code. If I put a standard ListBox on the page it's
selectedIndex is retained after postback with no additional code.
Thanks,
Don

Jul 21 '05 #1
1 2085
Make sure you're not populating the listbox in the page_load event for every
page request. You need to make sure you check the "IsPostBack" property
before populating the control. If you forget then the selected value will
not be shown in the listbox because you're initialized it again.

void Page_Load()
{
if (!IsPostBack)
{
//Initialize/DataBind List Box
}
}

If that doesn't work, try posting some of your code to the newsgroup.

HTH,
Jorge

"ch*********@hotmail.com" wrote:
I have created a Web Custom Control that inherits from
WebControls.ListBox but can't get it to retain the selectedIndex after
a postback. As a test I created a very simple version of the control
with no additional code. I just inherited from the exisiting ListBox
class and put the control on a page.
I put this control a blank page along with a button to perform a post
back. I select an item in the list box and then click the button and
after the postback I have lost the selected value. I added some
javascript to alert to the selectedIndex just to make sure the client
had one and it did.
EnableViewState is set to true for this control. If I make a custom
textbox in the same method it's value is retained after postback with
no additional code. If I put a standard ListBox on the page it's
selectedIndex is retained after postback with no additional code.
Thanks,
Don

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Russell Campbell | last post: by
1 post views Thread by bill yeager | last post: by
reply views Thread by fwirtanen | last post: by
8 posts views Thread by mark.norgate | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.