I have a problem for setting the Text property of a TextBox. Here some part of the code :

protected void Page_Load(object sender, EventArgs e)
{ 
    ...
    if (!Page.IsPostBack)
    {
        string termRequest = Request["term"];
        if (termRequest != "*:*")
        {
            TB_MotCle.Text = termRequest;
        }
    }
    ...
}

So I'm just trying to get a parameter from the URL and display the value in my TextBox. I have a button that validate the page and end up redirecting to the same page with parameters (including "term").

protected void BTN_Rechercher_Click(object sender, EventArgs e)
{
    ...
    if (string.IsNullOrEmpty(TB_MotCle.Text))
    {
        sb.Append("?term=*:*");
    }
    else
    {
        sb.Append(string.Concat("?term=", HttpUtility.UrlEncode(TB_MotCle.Text)));
    }
    ...
    Response.Redirect(sb.ToString());
}

So, the redirection calls the page without a PostBack, my first code in !Page.IsPostBack is called, my TextBox gets the new value. Everything is fine.

The problem is : If I use the History Back function from my navigator, the previous page is called, with the old "term" value, that's normal, the page is not a postback, my Code enters the TB_MotCle.Text = ...; (I checked in debug mode), but when the page refreshes I still get the value from the next page in the history.

I'm not sure if it's clear so here is what happens : I search on my page for "test1", click submit -> postback -> redirect with the value in my url -> page displays "test1". Then I do the same with "test2", page refreshes, I get "test2" in the TextBox. But then, if I press back on my navigator, the URL of the page contains "test1" (that's normal), I set that value to my TextBox in the PageLoad to "test1", but I still see "test2" in my TextBox.

I tried to disable the ViewState but it didn't work. What can I do ?

有帮助吗?

解决方案

This sounds like a caching problem rather than ViewState-related. Your browser is retrieving the version of the page immediately prior to navigation away, rather than the version it initially downloaded.

To fix this, you need to tell the browser not to cache the page; the SO question How to control web page caching, across all browsers? provides a number of solutions that should work across all browsers.

It will mean that someone navigating 'back' will be told to reload the page (and thus trigger your !Page.IsPostBack code), but that's the nature of switching caches off unfortunately.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top