Javascript enable on listbox does not work after postback

Hi there,
I am using ASP.NET 3.5 to create a form which has a checkbox and a list.

When the form loads, by default the checkbox is ticked and the list is disabled.

The theory is that if the checkbox is unticked then the listbox is enabled and the user is able to select items from the list.

The javascript works well in the fact that when the checkbox is unticked the list is enabled and the user is able to select items from the list.

However, when the form is submitted and I iterate through the items in the listbox none of the items show up as being selected.  If I start the form with the listbox as enabled then it all works fine.

If I check in the codebehind using breakpoints aI can see that even though the javascript has enabled the list the lbMyList.Enabled property is still "False".  It's like the .Enabled property isn't updated because it was only modified using javascript and not during a postback.

Is there any way to fix this?
### In the Page itself ###
<asp:CheckBox ID="cbEnableList" Text="Enable My List" Checked="true" runat="server" />
<asp:ListBox ID="lbMyList" Enabled="false" SelectionMode="Multiple" runat="server">
     <asp:ListItem Text="Test" Value="Test"></asp:ListItem>
### In the CodeBehind ###
### Add the Javascript to the page ###
If ClientScript.IsClientScriptBlockRegistered("EnableList") = False Then
                ClientScript.RegisterClientScriptBlock(Me.GetType, "EnableList", _
                    "<script type=""text/javascript"">" & vbCrLf _
                    & "function EnableList(theform) {" & vbCrLf _
                    & "   if (theform." & cbEnableList.ClientID & ".checked==true) {" & vbCrLf _
                    & "      theform." & lbMyList.ClientID & ".disabled = true;" & vbCrLf _
                    & "   }" & vbCrLf _
                    & "   else {" & vbCrLf _
                    & "      theform." & lbMyList.ClientID & ".disabled = false;" & vbCrLf _
                    & "   }" & vbCrLf _
                    & "}" & vbCrLf _
                    & "</script>" & vbCrLf _
            End If
cbEnableList.Attributes.Add("onclick", "EnableList(this.form);")
###  Iterating through the list after postback ###
If cbEnableList.checked = False Then
   For i As Integer = 0 To lbMyList.Items.Count - 1
      If lbMyList.Items(i).Selected = True Then      ' <--- Is always false
      End If
End If

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi there,
Yes you are right. It happens becuase you didn;t do a postback to enable the listbox so it is still disabled in code behind when a javscript enable it.
2 ways to solve it:
First is of course make you click even on checkbox a server side event and trigger a postback to change the enable property of listbox.
second way will be declare a hidden form field, add a javascript on your listbox that when selected the value, manually stored the data in the hidden field.
You can get the hidden field values in code behind using request.form()

Your problem was interresting. I had to think about it for awhile.

Another solution (tested and works) is to have ListBox enabled initially:
<asp:ListBox ID="lbMyList" Enabled="true" SelectionMode="Multiple" runat="server">

Disable the listbox via Javascript instead of on the server initially. I just used simple code for my test, you can add something similar to your clientscriptblock:

var lbMyList = document.getElementById('lbMyList');

Now the server is able to work with the list properly because it was not disabled initially.

With the addition of these small changes your code works "AS-IS". If this doesn't make sense let me know and I'll write something more explicit for you :)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SoLostAuthor Commented:
Thanks guys.  Having the boxes enabled initially and disabling via javascript when the page loads seems to have solved the problem for me.   I used the ClientScript.RegisterStartupScript method.

There is a period when the page first loads that the list can be seen to be enabled and selectable until the script runs but so long as noone is still using dial up I think I'll be ok :)

Thanks again
If you want to combat that "flicker" affect you might try having the list disabled initially:

<asp:ListBox ID="lbMyList" Enabled="false" SelectionMode="Multiple" runat="server">

Still use your diable code in JavaScript as before.

In page_loadcomplete on the server enable the listbox
lbMyList.enabled = true

I dunno if that will work I haven't tested it, but it seems like it might :)

Just a gentle reminder :)

I spent time to review and answer your question. It seems to be resolved. Can you assign the points please?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.