Search code examples
c#asp.netajaxscriptmanagermultiview

How can I register History Entries (Back/Forward) in a Multiview (on Active Index changed)


I'm using an update panel to have part of my page update asynchronously. Its navigation needs to be registered when a Multiview changes its active index.

For example, when the page first loads, the Multiview Active Index = 0. When user clicks on a link, I show the content of the View index 1.

It is really important to allow the user to go backward and forward using the browser history.

The ScriptManager is located in a master page and has its EnableHistory property set to True.

Has someone implemented it when working with Multiview?


Solution

  • I think this should work, but you'll need to customize it a bit. I'm doing something very similar, but I had to move away from using the multview within updatepanel due to a problem with a third-party control I had embedded within it.

    You'll need to add a reference to your Master page in your aspx file using the asp.net tag below. And also, expose the scriptmanager as a public control in your master page so you can wire an event to it.

    In your page's design/html code: Update the virtual path to your Master or use the Type to assign the type reference.

    <%@ MasterType VirtualPath="~/MasterPage.master" %>
    

    In your page's init event:

    public void Page_Init(object sender, EventArgs e)
    {
        // can be done at MasterPage level if you like
        this.Master.ScriptManager.EnableHistory = true;
    }
    

    Then in your page's load event:

    protected void Page_Load(object sender, EventArgs e)
    {              
        this.Master.ScriptManager.Navigate += 
        new EventHandler<HistoryEventArgs>(ScriptManager_Navigate);
    
        if (!this.IsPostBack && !ScriptManager.GetCurrent(this).IsInAsyncPostBack)
        {
            // load default multiview index
        }           
    }
    

    Then add this new event handler to your page. This names the item "myArgs", you should use something a bit more intuitive for your content. In my implementation, I have a series of items (like sort order, page index, etc. separated by delims and then break them up and assign them).

    protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.State["myArgs"]))
        {
            string args = e.State["myArgs"];
            SetMyArgs(args);                           
        }
        else
        {
            // just load default
        }
    }
    

    The args will be the multiview index. These are just helper functions that I use. If index is the only item you care about, then you can just inline these calls to the main methods.

    private void SetMyArgs(string args)
    {
        int myIndex = int.Parse(args);
        // set multiview index here?
    }
    
    private string GetMyArgs()
    {
        return myMultiview.ActiveIndex.ToString();
    }
    

    Then when you trigger the event that changes the active index, add this to those methods

    if (this.IsAsync)
    {
        this.Master.ScriptManager.AddHistoryPoint("myArgs", GetMyArgs());
    }
    

    Hopefully, this gives you some help.