Gridview redirect button and back button problem

My problem. I have an GridView inside an updatePanel. When I click in a button inside the gridview it redirects to another page. But when I click in back button in browser gets to the same page as before but with Session["Id"] changed. And it does PageLoad
when I click at any item inside the page. It there’s a way if I click back button that or redirect to first page or give me the same page as before with the same Sessions and not enter PageLoad??   thanks

//My ScriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableHistory="true" EnableSecureHistoryState="false"
        EnablePartialRendering="true" OnNavigate="ScriptManager1_Navigate">

//My update panel:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">

//My gridView:

 <asp:GridView ID="grid" runat="server" EnableModelValidation="true" AutoGenerateColumns="false"
                AllowPaging="true" PageSize="20" Width="100%" Font-Size="Smaller" OnRowDataBound="grid_RowDataBound"
                <HeaderStyle ForeColor="White" />
                    <asp:BoundField HeaderText="Name" DataField="name">
                        <ItemStyle />
                    <asp:BoundField HeaderText="id" DataField="id">
                        <ItemStyle />
                    <asp:BoundField HeaderText="AnotherField" DataField="anotherField">
                        <ItemStyle />
                    <asp:BoundField HeaderText="AnotherField2" DataField="anotherField">
                        <ItemStyle />
                            <asp:ImageButton ID="btnRedirect" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id") %>' CommandName="redirect"
                                ToolTip="Redirect" />

//My Code Behind:

 protected void ScriptManager1_Navigate(object sender, HistoryEventArgs e) // Event handler for restoring state
          Session["Id"] = e.State["value"];

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
            string id= Convert.ToString(e.CommandArgument);
            if (e.CommandName == "redirect")

                ScriptManager.GetCurrent(Page).AddHistoryPoint("value", idInThePage);
                Session["TipoDocumento"] = id;


You can try avoiding to go back from IE using the below code.

javascript: window.history.forward(1);

Refer the below link for more details using this with different browsers.

Hope this helps.


On your ScriptManager, you need to set EnableHistory="true". Then, on the buttons that change from page1 to page 2 (to print page #3), you have to invoke ScriptManager.GetCurrent(Page).AddHistoryPoint("KeyName", value);

This saves the back button history state.

Now you have to override the OnNavigate event of the ScriptManager (or ScriptManagerProxy if your ScriptManager is in a MasterPage). When navigating, you have to restore the user’s previous state manually.

The default behavior of the back button is what you are seeing — it goes to the previous URL loaded in the browser. When you are using UpdatePanels, this URL doesn’t change.

There are a few tutorials about this, here is one:

You can also google against ScriptManager, EnableHistory, AddHistoryPoint, and OnNavigate.

since the SP1 update for .NET 3.5 you have the History control for AJAX. This was specifically crafted for this task:Introduction to ASP.NET
AJAX History


thanks for your reply. I saw your anwser in other post. If you see the code I did everything what you said. But its not working. The onNavigate event never fires.

I did one solution but wasn’t perfect. I cleared the cache and kept every thing like i showed in my code above. Now everytime I click at the back button, the Page Load event is fired and It checks if the Session["Id"] is the same. Its not the same so it
redirects for the Initial Page.

I did my reaserch, read every post you gave. But it didn’t worked.  If need any more code please let me know.



I do not want to block the back button event, Its said to be bad practice.


But thanks for your reply.

Leave a Reply