473,835 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t sender, EventArgs e)
{
Location loc = new Location();

if (!IsPostBack)
{
SqlDataReader dr = (SqlDataReader) loc.GetCountryL ist();
lbxCountries.Da taSource = dr;
lbxCountries.Da taTextField = "CTRY_NAME" ;
lbxCountries.Da taValueField = "CTRY_CODE" ;
lbxCountries.Da taBind();
}

SqlDataReader drst = (SqlDataReader) loc.GetStateLis t
(lbxCountries.S electedItem.Val ue);
lbxState.DataSo urce = drst;
lbxState.DataTe xtField = "STAT_NAME" ;
lbxState.DataVa lueField = "STAT_CODE" ;
lbxState.DataBi nd();
}
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 1867
<cr************ @hotmail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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(objec t sender, EventArgs e)
{
Location loc = new Location();

if (!IsPostBack)
{
SqlDataReader dr = (SqlDataReader) loc.GetCountryL ist();
lbxCountries.Da taSource = dr;
lbxCountries.Da taTextField = "CTRY_NAME" ;
lbxCountries.Da taValueField = "CTRY_CODE" ;
lbxCountries.Da taBind();
SqlDataReader drst = (SqlDataReader) loc.GetStateLis t
(lbxCountries.S electedItem.Val ue);
lbxState.DataSo urce = drst;
lbxState.DataTe xtField = "STAT_NAME" ;
lbxState.DataVa lueField = "STAT_CODE" ;
lbxState.DataBi nd();
}


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

John Saunders
Nov 19 '05 #2
"John Saunders" wrote:
<cr************ @hotmail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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(objec t sender, EventArgs e)
{
Location loc = new Location();

if (!IsPostBack)
{
SqlDataReader dr = (SqlDataReader) loc.GetCountryL ist();
lbxCountries.Da taSource = dr;
lbxCountries.Da taTextField = "CTRY_NAME" ;
lbxCountries.Da taValueField = "CTRY_CODE" ;
lbxCountries.Da taBind();


SqlDataReader drst = (SqlDataReader) loc.GetStateLis t
(lbxCountries.S electedItem.Val ue);
lbxState.DataSo urce = drst;
lbxState.DataTe xtField = "STAT_NAME" ;
lbxState.DataVa lueField = "STAT_CODE" ;
lbxState.DataBi nd();
}


You don't want to call DataBind on PostBack. Instead, get the state list for
the country only when the country changes, in the SelectedIndexCh anged 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 SelectedIndexCh anged 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
5158
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
3
23778
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 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...
10
10775
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 = true; Where endTime is a string containing "15".
0
1175
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 take longer, my code behind will error out since I expect "ddlSample.SelectedItem.Value" to have a value for a control that exist. But sometimes it is set to "Nothing". I do think this comes about when the user is a 'quick clicker' and clicks...
2
2705
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 item will be selected in the second listbox too. The problem is, when the user selects an item and it is in the middle of the list, the user is not able to know whether it is selected or not. Is there a way to display all the selected items...
6
22183
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
5819
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
1
1824
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 ViewState("xxx") Is Nothing Then Return ViewState("xxx")
1
3513
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 language="javascript">
0
9802
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10807
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10517
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10559
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9343
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7765
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6961
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5802
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.