Dropdownlist selected value lost after postback

C

Celine

I have a webform with multiple panels with textboxes, dropdownlists,
radiobuttonlists, etc. The user navigates through the panels and then
submits the information at the end.

The first panel contains a text box where users can enter a search term. A
"next" button displays the following panel causing a stored procedure to
populate a dropdown list. The dropdown list displays the results of the
search, and requires users to select an option.

The problem is that when the user navigates forward to other panels, the
selected option in the dropdown list is not retained. The first entry in
the dropdownlist is what is submitted to the database, not the option the
user selected. This is because I am populating the list when
Page.IsPostBack. When I've tried to populate the list when ! IsPostBack is
true, it doesn't populate correctly. Instead of displaying options based on
the search term, it displays every item in the database. However, in this
scenario, the selected value is retained.



How can I get around this problem?



Here is the code from the codebehind page



if (Page.IsPostBack)

{



//populate dropOwnerSelect

SqlCommand cmdSelect;

SqlDataReader dtrOwner;



cmdSelect = new SqlCommand( "sp_selectCompanies", conProjData);

cmdSelect.CommandType = CommandType.StoredProcedure;

cmdSelect.Parameters.Add("@name", txtOwner.Text);

conProjData.Open();

dtrOwner = cmdSelect.ExecuteReader();



dropOwnerSelect.DataSource = dtrOwner;

dropOwnerSelect.DataValueField = "CompanyID";

dropOwnerSelect.DataTextField = "CompanyName";

dropOwnerSelect.DataBind();



dtrOwner.Close();

conProjData.Close();

}
 
J

James Thomas

The problem is that when the user navigates forward to other panels,
the selected option in the dropdown list is not retained. The first
entry in the dropdownlist is what is submitted to the database, not
the option the user selected. This is because I am populating the
list when Page.IsPostBack. When I've tried to populate the list when
! IsPostBack is true, it doesn't populate correctly. Instead of
displaying options based on the search term, it displays every item
in the database. However, in this scenario, the selected value is
retained.

You could store the value of the ListItem in question in the Session
state and then when after the form loads, you can do:

dropDown.SelectedValue = Session["dropDownValue"]

James
 
C

Celine McLean

But then session state changes after the form posts back again and the
selected item is not retained.

Is there another way of approaching this so that the value can be kept
throughout the postbacks to the form?
 
J

James Thomas

Celine said:
But then session state changes after the form posts back again and the
selected item is not retained.

Is there another way of approaching this so that the value can be kept
throughout the postbacks to the form?

Why change it on postback? I do something similar and I only change it
when a new item is selected from the drop down (or I in-code change it,
I just call my SelectionChanged event with null, null as arguments and
that method changes the Session variable, not the Page_Load method).

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top