views:

866

answers:

4

I have an asp.net form used for search. The search result is showing on the same page using Ajax. If I navigate to another page and come back, I get the populated form but the search result is empty. What's the best way to go back to the page and have the result populated.

Or how to force the page to post back to repopulate the page with the results when back button is clicked?

+3  A: 

There are a number of frameworks that attempmt to handle the back button and ajax.
Here is Microsoft 's
Here is one called really simple history

cptScarlet
+6  A: 

You can do this using the EnableHistory property on the ScriptManager. Once you've set the ScriptManager's EnableHistory property to True, you can use the AddHistoryPoint method to set history points in your page.

<asp:ScriptManager runat="server" ID="MyScriptManager" EnableHistory="True">
</asp:ScriptManager>

Dino Esposito has a good pair of articles here (client-side) and here (server-side) about using the scriptmanager history functionality.

Scott Ivey
A: 

I have implement the same with the following Article http://rchern.wordpress.com/2008/05/11/updatepanel-backforward-browser-navigation/
you can also check this thread and my answer..http://stackoverflow.com/questions/973739/how-can-i-get-the-same-page-with-the-click-of-back-button-of-browser/973753#973753

In this example I am showing you how to maintain gridview paging , When user click browser back button. First of all you have to Enable ScriptManager history EnableHistory="true"
Add in history point the changes made in AJAX updatepanel

 private void AddHistoryPoint(String key, String value, String tile)
{
    ScriptManager scm = ScriptManager.GetCurrent(this.Page);
    if ((scm.IsInAsyncPostBack == true) && (scm.IsNavigating != true))
    {
        if (pageState == null)
        {
            NameValueCollection pageState = new NameValueCollection();
        }
        if (pageState[key] != null)
        {
            pageState[key] = value;
        }
        else
        {
            pageState.Add(key, value);
        }
        scm.AddHistoryPoint(pageState, tile);
    }
}
protected void grid_PageIndexChanged1(object sender, EventArgs e)
{
    AddHistoryPoint("pi", grdProject.PageIndex.ToString(), "Page Index- " + (grdProject.PageIndex + 1).ToString());
}

here you have to handle ScriptManager Navigate Event

protected void ScriptManager1_Navigate(object sender, System.Web.UI.HistoryEventArgs e) 
{ 
    if (e.State != null) 
    { if (e.State["pi"] != null)
    {
        grid.PageIndex = Convert.ToInt32(e.State["pi"]); 
    }
    }
}
Muhammad Akhtar