简体   繁体   中英

ASP.NET Ajax History not working correctly

I have an ASP.NET/C# application.

The application containsa PlaceHolder inside an UpdatePanel .

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>    
        <asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click"/>
        <asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click"/>
    </Triggers>
    <ContentTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
        </asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

Basically I load controls in the placeholder depending on what LinkButton has been clicked using a function

    LoadControls("ControlName");

I want to implement the ability for the user to use the Browser Back Button in order to navigate through the controls. so I used AJAX History like this:

I save the current control ID in a session variable

Session["current"]

I enable History in scriptmanager and add an event handler

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

(The event handler will be covered below) First I will create a new history point in the event handler of the Linkbuttons (they all use the same handler)

string ControlId=Session["current"].ToString();

if (ScriptManager.IsInAsyncPostBack && !ScriptManager.IsNavigating)
{
    ScriptManager.AddHistoryPoint("Hist", ControlId);
}

then in the Scriptmanager navigate handler

    protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
    {
        string Controlid = "";
        if (e.State["index"] != null)
        {
            Controlid = e.State["Hist"].ToString();
            LoadControls(Controlid );
        }
    }

When I load the controls I can navigate back and forward and everything is working fine

Here is the problem:

1) first problem

The first time I click on "Back". the history returns 2 steps and not one (after that it works normaly.) It is like the last history is not saved.

2) second problem

After "Backing" to the first step I can do an extra "Back" where the e.State["Hist"] will be null and give me an error. how can I disable the "Browser Back" button when the e.State["Hist"] will be null?

Thank you very much for any help. I hope I was clear.

If you need more info/code I will gladly provide them

Never mind. it was just a bug. the Session["Current"] was saving the previous control and not the current one. It is now fixed. I will leave this question if somebody needs a small example for creating ajax history.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM