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">
    </asp:ScriptManager>


//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"
                OnPageIndexChanging="grid_PageIndexChanging"
                OnRowCommand="grid_RowCommand">
                <HeaderStyle ForeColor="White" />
                <Columns>
                    <asp:BoundField HeaderText="Name" DataField="name">
                        <ItemStyle />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="id" DataField="id">
                        <ItemStyle />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="AnotherField" DataField="anotherField">
                        <ItemStyle />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="AnotherField2" DataField="anotherField">
                        <ItemStyle />
                    </asp:BoundField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:ImageButton ID="btnRedirect" runat="server" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"id") %>' CommandName="redirect"
                                ToolTip="Redirect" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>




//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;
                Response.Redirect("RedirectPage.aspx");
            }
        }

Hi

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.

http://go4answers.webhost4life.com/Example/windowhistoryforward1-historygo1-159725.aspx

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:

http://geekswithblogs.net/ranganh/archive/2008/11/17/enabling-the-browserrsquos-back-button-for-grid-view-asp.net-ajax.aspx

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
.

http://quickstarts.asp.net/Futures/ajax/doc/history.aspx

http://weblogs.asp.net/brijmohan/archive/2008/09/11/using-asp-net-3-5-history-control-with-asp-net-2-0.aspx

http://www.nikhilk.net/BackButtonSupport.aspx

Hi,

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.

 

Thanks

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

 

But thanks for your reply.

Leave a Reply