Question

I have a weird problem with the DropDownList postback.

I have a DropDownList in asp.net master page, which contains some state names like :

  1. Text [NewYork] - Value [0]
  2. 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

Was it helpful?

Solution

This is normal behavior. When you select the dropdownlist item, it posts back, loads the content page first, runs:

Response.Write("Default Town Is: " + Globals.DefaultTown + "<br />");

Here Globals.DefaultTown did not change yet.

Then it goes to rpTowns_SelectedIndexChanged method and changes the Globals.DefaultTown.

This page may help to understand better:Events in ASP.NET Master and Content Pages.

SOLUTION:

1.If there are no side effects, you can move the code to the Masterpage:

protected void drpTowns_SelectedIndexChanged(object sender, EventArgs e)
{
    Globals.DefaultTown = Convert.ToInt32(drpTowns.SelectedValue);
    Response.Write("Default Town Is: " + Globals.DefaultTown + "<br />");
}

2.Or you can redirect to the same page. In the masterpage:

protected void drpTowns_SelectedIndexChanged(object sender, EventArgs e)
{
    Globals.DefaultTown = Convert.ToInt32(drpTowns.SelectedValue);
    Response.Redirect(Request.RawUrl);
}

In the content page:

if (!IsPostBack)
{
    Response.Write("Default Town Is: " + Globals.DefaultTown + "<br />");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top