By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,357 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,357 IT Pros & Developers. It's quick & easy.

SelectedIndexChanged in drop down - How to get the newly selected value

P: 10
I am using ASP.Net 2.0 and C# and new to this.

I have programmatically added a zip code drop down into web form inside Page_Load method as:
ZipDropDown.Items.Add("11111");
ZipDropDown.Items.Add("22222");

ZipDropDown is the drop down control on the web page.
I have created a protected void ZipDropDown_SelectedIndexChanged(object sender, EventArgs e) method in my corresponding .cs file for the .aspx file.

protected void ZipDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string a = this.ZipDropDown.SelectedItem.Text;
string b = this.ZipDropDown.SelectedItem.Value;
}

I have set AutoPostBack as true for ZipDropDown control so that the SelectedIndexChanged gets triggered when the user changes zip code from the drop down.

My problem is, in the above code, variables a and b are not getting the correct value when the selection is changed from 11111 to 22222..
The page was populated with the value of the 11111 which is being displayed in the zip code. Then I selected 22222 from the drop down. The ZipDropDown_SelectedIndexChanged got triggered. But the variable a and b still show the value 11111 (old value for drop down), not the newly selected value 22222.

How can I get the newly selected value of 2222 from the dropdown on the server side? How can I also ensure that that new value of 2222 stays displayed in the browser in the drop down?
Dec 15 '06 #1
Share this Question
Share on Google+
2 Replies


shweta123
Expert 100+
P: 692
Check for isPostback
If Page.IsPostBack = False Then
ZipDropDown.Items.Add("11111");
ZipDropDown.Items.Add("22222");
End if

It should work.


Shweta
Dec 18 '06 #2

radcaesar
Expert 100+
P: 759
Use the If (! IsPostBack)

Then,
Set the ValueMember property of combo to the Member u want.

Get the value using SelectedValue propeerty.

:)
Dec 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.