?
Solved

How to prevent databound controls losing their selected items on postback

Posted on 2006-06-06
7
Medium Priority
?
296 Views
Last Modified: 2012-06-27
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
Comment
Question by:penlandt
  • 5
  • 2
7 Comments
 
LVL 1

Author Comment

by:penlandt
ID: 16848535
Should have mentioned this is an ASP.NET 2.0 App
0
 
LVL 16

Accepted Solution

by:
Edwin_C earned 2000 total points
ID: 16848647
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
 
LVL 1

Author Comment

by:penlandt
ID: 16848732
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:penlandt
ID: 16848784
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
 
LVL 1

Author Comment

by:penlandt
ID: 16848931
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
 
LVL 16

Expert Comment

by:Edwin_C
ID: 16848946
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
 
LVL 1

Author Comment

by:penlandt
ID: 16849104
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question