Preserving the selected item between postbacks.

C

craigkenisston

Hi,

I am programming a classical registration form using asp.net.
I have the country list and a list of all states for each country in a
database.

I already have this working code in which if the user selects a country
I populate a list with the states/provinces for that country, the
dropdown for the countries is set to autopostback.

protected void Page_Load(object sender, EventArgs e)
{
Location loc = new Location();

if (!IsPostBack)
{
SqlDataReader dr = (SqlDataReader)loc.GetCountryList();
lbxCountries.DataSource = dr;
lbxCountries.DataTextField = "CTRY_NAME";
lbxCountries.DataValueField = "CTRY_CODE";
lbxCountries.DataBind();
}

SqlDataReader drst = (SqlDataReader)loc.GetStateList
(lbxCountries.SelectedItem.Value);
lbxState.DataSource = drst;
lbxState.DataTextField = "STAT_NAME";
lbxState.DataValueField = "STAT_CODE";
lbxState.DataBind();
}


However, when there's a problem in the form, for example, a missing
field, and I show the registration form again the state list is reset
to the first item in the list, i.e. it losses the position it has prior
to the attemp of registration.
So, how should I program this in order to kept the current item of the
lbxState between postbacks ?
 
J

John Saunders

Hi,

I am programming a classical registration form using asp.net.
I have the country list and a list of all states for each country in a
database.

I already have this working code in which if the user selects a country
I populate a list with the states/provinces for that country, the
dropdown for the countries is set to autopostback.

protected void Page_Load(object sender, EventArgs e)
{
Location loc = new Location();

if (!IsPostBack)
{
SqlDataReader dr = (SqlDataReader)loc.GetCountryList();
lbxCountries.DataSource = dr;
lbxCountries.DataTextField = "CTRY_NAME";
lbxCountries.DataValueField = "CTRY_CODE";
lbxCountries.DataBind();

SqlDataReader drst = (SqlDataReader)loc.GetStateList
(lbxCountries.SelectedItem.Value);
lbxState.DataSource = drst;
lbxState.DataTextField = "STAT_NAME";
lbxState.DataValueField = "STAT_CODE";
lbxState.DataBind();

You don't want to call DataBind on PostBack. Instead, get the state list for
the country only when the country changes, in the SelectedIndexChanged event
for lbxCountries.

John Saunders
 
G

Guest

John Saunders said:
SqlDataReader drst = (SqlDataReader)loc.GetStateList
(lbxCountries.SelectedItem.Value);
lbxState.DataSource = drst;
lbxState.DataTextField = "STAT_NAME";
lbxState.DataValueField = "STAT_CODE";
lbxState.DataBind();


You don't want to call DataBind on PostBack. Instead, get the state list for
the country only when the country changes, in the SelectedIndexChanged event
for lbxCountries.

John Saunders

Since you are binding your select box on each request, you are overriding
the viewstate maintained by ASP.NET. And hence everytime you get fresh data
in your select box.

As pointed by John, tt is not required to bind data with every request. You
can implement SelectedIndexChanged event and put the binding logic there. So,
the select box can maintain its viewstate on other postbacks.

Cheers,
Rahul Anand
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top