I have a weird 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.GetAllStates();
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
{
if (HttpContext.Current.Session["DefaultTown"] == null)
return _defaultTown;
else
return Convert.ToInt32(HttpContext.Current.Session["DefaultTown"]);
}
set
{
HttpContext.Current.Session["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
");` to the method in masterpage. I know it is not very convenient, because you may need to do other things in content page level also. If you implement it in masterpage, you are forcing all content pages to use this, which is not okay for all cases. – afzalulh Sep 15 '13 at 18:52