It is very strange. I have a javascript function to enable the listbox which is disable when it loaded. This javascript function works fine to enable or disable the listbox. However after the user click the save button, it didn’t get the new selected item; always keep the old selected item by databind. I checked the isPostback on Page load. If it is not isPostback, the listbox is not load again. Would someone give me the hint to solve this problem. Thansk in advance.
function enableProject() {
var chk=document.getElementById('chkProject');
if (chk.checked) {
document.getElementById('listProject').disabled = false;
}
else {
document.getElementById('listProject').disabled = true;
}
}
There is my asp.net page
<asp:ListBox id="listProject" runat="server"
SelectionMode="Multiple" Rows="5" class="w200" Enabled="false"/>
<asp:checkbox id="chkProject" runat="server"
Text="Enable" onclick="enableProject()" />
The following code is for check the ListBox:
For Each projectItem In listProject.Items
If projectItem.Selected Then
'create a new project object
Dim objProj As New Data.Project( ID, projectItem.Value)
objProjects.Add Project(objProj)
End If
Next
First thing to check is that our not ‘rebinding’ on each postback. If you are that would explain why your getting what you started with. Can you post your code where you are binding the
ListBox?If the control is disabled on postback then it will not post ‘new’ values and it will just go off of what the ViewState had stored originally for the contrl so make sure that it truely is enabled before the postback.
Here is a link with an example of that behaviour:
https://web.archive.org/web/20210420005338/https://aspnet.4guysfromrolla.com/demos/ProblemNotReturningDisabledControls.aspx
If you want to test if this is the problem set the following property in your form tag which will force disabled controls to be posted back: