在asp.net中从Postback上的DropDownList中检索值

时间:2022-03-19 15:56:05

I have a weird problem with the DropDownList postback.

我对DropDownList回发有一个奇怪的问题。

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

我在asp.net母版页中有一个DropDownList,它包含一些状态名称,如:

  1. Text [NewYork] - Value [0]
  2. 文字[纽约] - 价值[0]
  3. Text [New Jersey] - Value [1]
  4. 文字[新泽西] - 价值[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

在母版页后面的代码中,我有一个DropDownList_SelectedIndexChanged事件,我在设置持有会话的变量中的下拉列表的SelectedValue。如下

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:

Globals.DefaultTown的定义是用App_Code Globals.cs类编写的,如下所示:

 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:

现在我想在内容页面(Default.aspx)中检索Globals.DefaultTown的值。我这样做如下:

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.

现在每当我从下拉列表中选择状态时,Globals.DefaultTown不会立即更新,就像默认情况下为DefaultTown设置Selected State,但是当我从列表中选择第二个状态时,它仍然会给出第一个状态的id,现在当我从列表中选择第三个状态时,它给出第二个的id,当我从列表中选择第一个状态时,它给出第三个状态的id,即它不会在现场更新DefaultTown变量。

Can anyone tell me what would be going wrong for this

任何人都可以告诉我这会出现什么问题

1 个解决方案

#1


1  

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.

这里Globals.DefaultTown还没改变。

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

然后转到rpTowns_SelectedIndexChanged方法并更改Globals.DefaultTown。

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

此页面可能有助于更好地理解:ASP.NET主页和内容页面中的事件。

SOLUTION:

解:

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

1.如果没有副作用,您可以将代码移动到母版页:

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:

2.或者您可以重定向到同一页面。在主页中:

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 />");
}

#1


1  

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.

这里Globals.DefaultTown还没改变。

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

然后转到rpTowns_SelectedIndexChanged方法并更改Globals.DefaultTown。

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

此页面可能有助于更好地理解:ASP.NET主页和内容页面中的事件。

SOLUTION:

解:

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

1.如果没有副作用,您可以将代码移动到母版页:

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:

2.或者您可以重定向到同一页面。在主页中:

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 />");
}