• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

How to prevent databound controls losing their selected items on postback

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
        With Me
            If .lstModels.SelectedValue = "" Then
                .cmdEdit.Enabled = False
                .cmdEdit.Visible = False
            Else
                .cmdEdit.Enabled = True
                .cmdEdit.Visible = True
                'Clear error messages, add, and update text boxes
                .lblMessage.Visible = False
                .txtModelAdd.Text = ""
                .txtModelUpdate.Text = ""
            End If
        End With
    End Sub
0
penlandt
Asked:
penlandt
  • 5
  • 2
1 Solution
 
penlandtAuthor Commented:
Should have mentioned this is an ASP.NET 2.0 App
0
 
Edwin_CCommented:
Check the Page_Load again and make sure you DataBind() the dropdownlist only when the page is first loaded but not in subsequent postback.

If not IsPostBack Then
   bind you databound controls
End if
0
 
penlandtAuthor Commented:
So I should NOT use the "configure data source" option built in to the listbox control?  Am I understanding correctly?  Because if I do, I don't see how I can keep it from binding every time the page loads.  I'm still trying to get out of my desktop application mindset and these postbacks really confuse me sometimes.  Thanks for the help.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
penlandtAuthor Commented:
I still need to find out what I'm doing wrong - because if with the Page.IsPostBack check, I'm still losing the selected values in both controls:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            MakesBindData()
        Else
            ModelsBindData()
        End If
    End Sub

    Protected Sub MakesBindData()
        cboMakes.DataSource = SqlDataSource1
        cboMakes.DataTextField = "Description"
        cboMakes.DataValueField = "ID"
        cboMakes.DataBind()
    End Sub

    Protected Sub ModelsBindData()
        lstModels.DataSource = SqlDataSource2
        lstModels.DataTextField = "Description"
        lstModels.DataValueField = "ID"
        lstModels.DataBind()
    End Sub

0
 
penlandtAuthor Commented:
Sorry to bug you with that last question...I need to take a break...the answer is obviously that I need to check whether the listbox has a SelectedValue before binding the data.  Thanks again for your help.
0
 
Edwin_CCommented:
It does not make sense.  Make sure you did not reset the selectedindex such as (cboMakes.SelectedIndex = -1) somewhere in the code.  Also check EnableViewState=true in the your dropdownlist and listbox.

There is not trick behind this.  You can easily demonstrate it by adding a dropdownlist and a button in a form.  The dropdownlist will not be reset after clicking the button if you do not rebind it again.
0
 
penlandtAuthor Commented:
Thanks again.  I figured out what I was doing wrong and fixed it.  It's working great now (see my previous comment).  I appreciate the help very much.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now