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?
ddayx10Connect With a Mentor Commented:
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 :)

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

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?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.