Sunday, 15 September 2013

Retreiving values from DropDownList on Postback in asp.net

Retreiving values from DropDownList on Postback in asp.net

I have a weired problem with the DropDownList postback.
I have a DropDownList in asp.net master page, which contains some state
names like :
Text [NewYork] - Value [0]
Text [New Jersey] - Value [1]
drpTowns.DataSource = objTown.GetAllTowns();
drpTowns.DataTextField = "Name";
drpTowns.DataValueField = "Id";
drpTowns.DataBind();
In the code behind of the master page, i have a
DropDownList_SelectedIndexChanged event, where i am setting the
SelectedValue of the dropdownlist in a variable which is holding session.
like below
protected void drpTowns_SelectedIndexChanged(object sender, EventArgs e)
{
Globals.DefaultTown = Convert.ToInt32(drpTowns.SelectedValue);
}
Definition for Globals.DefaultTown is written in App_Code Globals.cs Class
like below:
private static int _defaultTown =
Convert.ToInt32(ConfigurationManager.AppSettings["DefaultTown"]);
public static int DefaultTown
{
get
{
return _defaultTown;
}
set
{
_defaultTown = value;
}
}
Now i want to retrieve the value of the Globals.DefaultTown in Content
Page (Default.aspx). I am doing that like below:
Response.Write("Default Town Is: " + Globals.DefaultTown + "<br />");
Now whenever i selects the state from the dropdownlist, the
Globals.DefaultTown does not updates immediately, like by default the
Selected State is setted for DefaultTown, but when i selects second state
from the list, it still gives the id of first state, now when i select
third state from the list, it gives the id of the second, and when i
select first state from the list, it gives the id of the third state, i.e
it does not updates the DefaultTown variable on the spot.
Can anyone tell me what would be going wrong for this

No comments:

Post a Comment