How to prevent databound controls losing their selected items on postback
Posted on 2006-06-06
I understand this is a common problem but I can't find anything that specifically relates to my problem.
I have a dropdown listbox that contains the names of various automobile manufacturers. When the user selects a manufacturer, a listbox on the form populates with auto models produced by the selected manufacturer. When the user clicks on a model, I want to enable several command buttons on the form. The problem arises during postback when the selected items in the dropdown listbox and the regular listbox are lost - plus the command buttons do not become enabled. Here's a code sample. Thanks for any help you can provide.
Protected Sub lstModels_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstModels.SelectedIndexChanged
On Error Resume Next
If .lstModels.SelectedValue = "" Then
.cmdEdit.Enabled = False
.cmdEdit.Visible = False
.cmdEdit.Enabled = True
.cmdEdit.Visible = True
'Clear error messages, add, and update text boxes
.lblMessage.Visible = False
.txtModelAdd.Text = ""
.txtModelUpdate.Text = ""