重定向时记住 DropDownList 状态

发布于 2024-10-17 19:12:41 字数 1137 浏览 1 评论 0原文

我有 DropDownList,其中有一些选项可供选择。现在,当用户选择其中一个选项后,必须单击一个按钮,这是同一页面上的重定向:

protected void Button4_Click(object sender, EventArgs e)
{
    Response.Redirect("Graphs.aspx?Selection=" + DropDownList1.SelectedValue + "&Date1=" + TextBox1.Text + "&Date2=" + TextBox2.Text);
}

我必须使用重定向而不是回发,因为我使用 flot,它是 javascript 库,并且所有代码都必须当用户选择其他选项时再次写入。问题是,当它重定向回来时,由于某种原因,我无法让它记住 DropDownList 中的值,该值是用户之前选择的。我用这段代码尝试了这一点:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["Selection"] == "TemperatureOUT")
    {
        DropDownList1.SelectedIndex = 0;
    }
    else if (Request.QueryString["Selection"] == "Dewpoint")
    {
        DropDownList1.SelectedIndex = 1;
    }
}

问题是,如果用户从 DropDownList 中选择 Dewpoint,然后单击一个按钮,这会导致重定向,新的 url 应该有:

Graphs.aspx?Selection=Dewpoint&...

但是,它说:

 Graphs.aspx?Selection=TemperatureOUT&...

现在,如果我从 Page_Load 中删除该代码它有效,但它不记得用户之前选择了什么。现在我知道Page_Load 一定有什么东西。是不是当页面重定向时,会调用 Page_Load,因此它应该查找 Selection 值并将其打印出来。所以这一定是重定向的问题,由于某种原因没有设置选定的值。

I have DropDownList, which has some options to choose from. Now after when user selects one of the options, it has to click on a button, which is a redirection on the same page:

protected void Button4_Click(object sender, EventArgs e)
{
    Response.Redirect("Graphs.aspx?Selection=" + DropDownList1.SelectedValue + "&Date1=" + TextBox1.Text + "&Date2=" + TextBox2.Text);
}

I have to use redirection instead of postback, because I use flot which is javascript library and all the code has to be written again when user selects some other option. The problem is, when it redirects back, for some reason I can't make it to remember the value in the DropDownList, which has user previously selected. I tried this with this code:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["Selection"] == "TemperatureOUT")
    {
        DropDownList1.SelectedIndex = 0;
    }
    else if (Request.QueryString["Selection"] == "Dewpoint")
    {
        DropDownList1.SelectedIndex = 1;
    }
}

The problem is, if for example user selects Dewpoint from DropDownList and then clicks on a button, which causes redirection, the new url should have:

Graphs.aspx?Selection=Dewpoint&...

but instead, it says:

 Graphs.aspx?Selection=TemperatureOUT&...

Now if I remove that code from Page_Load it works, but it does not remember what has user selected before. Now I know that there has to be something with Page_Load. Isn't that when page is redirected, the Page_Load get's called, so it should look a the Selection value and print it out. So it has to be a problem with redirection, that for some reason does not set the selected value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老旧海报 2024-10-24 19:12:41

可能这就是您要求的

DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");

尝试这样写

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList ddl = (DropDownList)Page.PreviousPage.FindControl("Your first page drop down id here");
        if (ddl!=null)
        {
          if(ddl.SelectedValue == "2")
            // do your coding here
        }
    }
}

May be this is the one you are asking for

DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");

Try by writing like this

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList ddl = (DropDownList)Page.PreviousPage.FindControl("Your first page drop down id here");
        if (ddl!=null)
        {
          if(ddl.SelectedValue == "2")
            // do your coding here
        }
    }
}
深海夜未眠 2024-10-24 19:12:41

我要检查的第一件事是您正在使用 IsPostBack 来确定要添加到列表中的项目:

if (!IsPostBack)
{
    DropDownList1.Items.Add(...)
}

如果您对列表进行更改而没有安全地确定它应该是 GET 还是 POST 操作,则列表项将得到反弹(假设您将它们添加到代码而不是标记中)。当页面通过 POST 加载时,它会恢复控件的状态(在本例中是添加的项目),然后在处理加载事件之前应用任何更改(如所选项目等)。

当您执行重定向时,您将向重定向目标发出新的 GET 请求,这将呈现页面而不处理选择。结合使用 IsPostBack 和处理 GET 请求上的查询字符串,应该足以解决您的问题。

The first thing I would check is that you are using IsPostBack to determine what items to add to your list:

if (!IsPostBack)
{
    DropDownList1.Items.Add(...)
}

If you make changes to the list without safely determining whether it should be a GET or POST operation, the list items will get rebound (assuming you adding them in code not markup). When the page loads through POST, it restores the state of the controls (that being the items added in this case), and then applies any changes (like selected item, etc.) before handling the load event.

When you do a redirect, you are issuing a new GET request for the redirect target, which will render the page without processing the selection. With a combination of IsPostBack and handling the querystring on a GET request, that should be enough to solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文