Update TextBox Value on history back

Stephane Mathis

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 ?

Adrian Wragg

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update textbox value $on event

From Dev

Jquery val() does not bring back value of textbox

From Dev

Update scope value after dynamically update textbox

From Dev

What is the right workflow when applying an update to a git patch back in history

From Dev

how to use the listbox value to update textbox

From Dev

HTML textbox default value update with every input

From Dev

Update textbox value when a button is clicked

From Dev

Entity framework update with textbox.text value

From Dev

I want to update specific textbox value

From Dev

Update textbox value when a button is clicked

From Dev

Update certain column of recordset from textbox value

From Dev

I want to update specific textbox value

From Dev

How to clear text field value on history.back()

From Dev

TextBox loses value after post back with TextMode Number

From Dev

SQL update statement setting the same value back

From Dev

Update the value from table in textbox BUT not updated the value in db?

From Dev

Add browser back history

From Dev

back link but not use history

From Dev

Style History Back Button

From Dev

Update Value In textbox using Knockout + Typeahead Custom Binding

From Dev

Update TextBox text when a ComboBox's value is changed

From Dev

Why won't knockout update the value in my textbox?

From Dev

Update textBox.Text default value on a button click in WinForms

From Dev

jQuery UI slider and textbox value 2-way update

From Dev

Update textBox.Text default value on a button click in WinForms

From Dev

Update value in another form's textbox via VBA

From Dev

Live update php variable and simultaneously, show the value in a textbox

From Dev

Update textbox value based on combox selection in uwp using mvvm

From Dev

How to update the textbox value based on selectbox using JavaScript?

Related Related

  1. 1

    Update textbox value $on event

  2. 2

    Jquery val() does not bring back value of textbox

  3. 3

    Update scope value after dynamically update textbox

  4. 4

    What is the right workflow when applying an update to a git patch back in history

  5. 5

    how to use the listbox value to update textbox

  6. 6

    HTML textbox default value update with every input

  7. 7

    Update textbox value when a button is clicked

  8. 8

    Entity framework update with textbox.text value

  9. 9

    I want to update specific textbox value

  10. 10

    Update textbox value when a button is clicked

  11. 11

    Update certain column of recordset from textbox value

  12. 12

    I want to update specific textbox value

  13. 13

    How to clear text field value on history.back()

  14. 14

    TextBox loses value after post back with TextMode Number

  15. 15

    SQL update statement setting the same value back

  16. 16

    Update the value from table in textbox BUT not updated the value in db?

  17. 17

    Add browser back history

  18. 18

    back link but not use history

  19. 19

    Style History Back Button

  20. 20

    Update Value In textbox using Knockout + Typeahead Custom Binding

  21. 21

    Update TextBox text when a ComboBox's value is changed

  22. 22

    Why won't knockout update the value in my textbox?

  23. 23

    Update textBox.Text default value on a button click in WinForms

  24. 24

    jQuery UI slider and textbox value 2-way update

  25. 25

    Update textBox.Text default value on a button click in WinForms

  26. 26

    Update value in another form's textbox via VBA

  27. 27

    Live update php variable and simultaneously, show the value in a textbox

  28. 28

    Update textbox value based on combox selection in uwp using mvvm

  29. 29

    How to update the textbox value based on selectbox using JavaScript?

HotTag

Archive