473,416 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Preserving the selected item between postbacks.

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 ?

Nov 19 '05 #1
3 1852
<cr************@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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
Nov 19 '05 #2
"John Saunders" wrote:
<cr************@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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


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
Nov 19 '05 #3
John & Rahul :
Thanks a lot, that's made the work !

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
3
by: Celine | last post by:
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...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
0
by: jhonz | last post by:
I have come across a strange circumstance. I have user controls with dropdownlists that interact fine during times where the web server has minimal load. But once things pick up and postbacks...
2
by: Srimadhi | last post by:
Displaying selected items at the top of the listbox Hi, I am having two listboxes - one with ids and second with the related names. When user selects an item in one listbox, the corresponding...
6
by: George | last post by:
Hi all, How can I get the value stored from the selected item and subitems of a listview? Thanks in advance, George
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
by: Joe Abou Jaoude | last post by:
hi, I just want simply to preserve some data in my usercontrol on postbacks. so I added this property in my UserControl Protected Property MyProperty() As String Get If Not...
1
by: Falcula | last post by:
Hello, I have a treeview control, when i select a item i navigate to url. But selected node is lost, it reset itself, loosing state. I post my code here. Thanks in advance. <script...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.