Verwalten des Browserverlaufs mithilfe von ASP.NET-Serversteuerelementen

Aktualisiert: November 2007

Als Seitenentwickler können Sie mit dem ScriptManager-Serversteuerelement und dem ScriptManagerProxy-Serversteuerelement Browserverlaufseinträge verwalten und logische Navigation bereitstellen. Mit dem ScriptManager-Serversteuerelement können Sie Verlaufspunkte in der Anwendung festlegen. Mit beiden Steuerelementen können Sie die Navigation verwalten, die erfolgt, wenn die Website infolge eines Verlaufszustands angefordert wird.

Ein Verlaufspunkt ist ein logischer Navigationspunkt in der Webanwendung, der über Zustandsinformationen dargestellt werden kann. Die Zustandsinformationen können verwendet werden, um einen vorherigen Zustand der Webanwendung wiederherzustellen. Dies erfolgt entweder direkt mit Zustandsdaten oder durch einen Bezeichner für Zustandsinformationen, die an einer anderen Stelle gespeichert sind.

Verlaufspunkte werden im Verlaufsstapel des Browsers nur als URLs gespeichert. Der Verlaufszustand wird als Daten in einer Abfragezeichenfolge oder als URL-Fragmentwert verwaltet, der durch das Zeichen "#" gekennzeichnet ist. Aufgrund von Größenbeschränkungen für URLs müssen die Zustandsinformationen, die Sie erstellen, so klein wie möglich sein. Im folgenden Beispiel wird eine URL dargestellt, die genügend Verlaufspunktdaten enthält, um den Zustand zu identifizieren. Anhand dieser Daten kann die Anwendung die Seite in diesem Zustand erneut erstellen.

http://MySamples/Ajax/Default.aspx#&&state=2

Wenn ein Benutzer im Browser auf die Schaltfläche Zurück klickt, navigiert der Browser durch zuvor angezeigte URLs. Diese umfassen URLs mit einem Verlaufspunktzustand. Client-Code in der Website erkennt, dass die URL Verlaufszustandsdaten enthält und führt eine Anforderung an die Seite aus. Dabei werden die Zustandsinformationen übergeben. Wenn die Seite die Anforderung verarbeitet, liest sie die Verlaufszustandsinformationen und löst ein asynchrones Postback aus. Anschließend lösen das ScriptManager-Serversteuerelement und das ScriptManagerProxy-Serversteuerelement das Navigate-Ereignis aus. Sie können dieses Ereignis behandeln und die Seite gemäß den Anforderungen für die Webanwendung erneut erstellen.

Hinweis:

Um den Beispielcode in diesem Thema zu erstellen, benötigen Sie Visual Studio 2008 Service Pack 1 oder höher.

Syntax für das ScriptManager-Steuerelement und das ScriptManagerProxy-Steuerelement

Im folgenden Beispiel wird die Syntax des ScriptManager-Serversteuerelements zum Arbeiten mit dem Browserverlauf dargestellt.

<asp:ScriptManager  
    EnableHistory="true|false"
    EnableStateHash="true|false"
    OnNavigate="navigateEventhandlerName">
</asp:ScriptManager>

Aktivieren der Browserverlaufsverwaltung

Um die Verlaufsverwaltung zu verwenden, müssen Sie sie durch das ScriptManager-Serversteuerelement aktivieren. Standardmäßig ist die Verlaufsunterstützung nicht aktiviert. Wenn der Verlauf aktiviert wird, wird er für jeden Browser anders implementiert. Für Internet Explorer wird ein iframe-Element für den Browser gerendert, das eine zusätzliche Anfrage an den Server auslösen kann. Daher ist das Modell ein Opt-In-Ansatz. Im folgenden Beispiel wird gezeigt, wie der Verlauf deklarativ durch das ScriptManager-Steuerelement aktiviert wird.

<asp:ScriptManager  ID="ScriptManager1" 
    EnableHistory="true" />

Erstellen von Browserverlaufspunkten

Um einen Browserverlaufspunkt zu erstellen, rufen Sie die AddHistoryPoint-Methode des ScriptManager-Steuerelements auf. Mit dieser Methode können Sie den Serverzustand und Schlüssel sowie optionale Daten definieren, um den Titel des Verlaufseintrags im Browser darzustellen. Sie können die Zustandsdaten verwenden, um den Status der Seite erneut zu erstellen, wenn ein nachfolgendes Verlaufsnavigationsereignis ausgelöst wird. Wenn Sie einen Verlaufspunkt erstellen, werden standardmäßig serialisierte und verschlüsselte Daten an die URL der Webseite angehängt. Die daraufs resultierende URL ist im Verlaufsstapel des Browsers enthalten.

Hinweis:

Fügen Sie Verlaufspunkte nur als Reaktion auf Benutzeraktionen hinzu, wie z. B. das Klicken auf eine Auswahl. In der Regel werden Verlaufspunkte nicht nur infolge der Ausführung von Anwendungscode hinzugefügt.

Im folgenden Beispiel wird das UpdatePanel-Steuerelement verwendet, um asynchrone Postbacks auf der Seite zu aktivieren. Das ScriptManager-Steuerelement wird verwendet, um während des Click-Ereignishandlers der Schaltflächen, die die asynchronen Postbacks auslösen, Verlaufspunkte hinzuzufügen. Wenn Sie im Browser auf die Schaltfläche Zurück klicken, verlassen Sie die Webseite nicht, sondern navigieren durch die früheren Verlaufszustände der Seite.

Führen Sie ein Beispiel für dieses Feature aus.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Collections.Generic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    Private Shared key As String = "s"

    ' Handle the Navigate event
    Public Sub OnNavigateHistory(ByVal sender As Object, ByVal e As HistoryEventArgs)
        LabelHistoryData.Text = Server.HtmlEncode(e.State(key))
    End Sub

    ' On button click, handle the event and set a history point. 
    Public Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        LabelHistoryData.Text = CType(sender, Button).Text
        ScriptManager.GetCurrent(Me).AddHistoryPoint(key, LabelHistoryData.Text)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code>event.<br />
                    </li>
                </ol>
            </div>

            <p>
                In this example, three buttons outside the <code>UpdatePanel</code> control can
                trigger an asynchronous postback. You can see the results of the update through
                the date and time that renders in the Web page along with a value indicating the 
                button that triggered the partial refresh.</p>
            <p>
                When you press a button, the server-side <code>Click</code> event handler for the button 
                stores data and uses the data as a history point. If you click the browser's Back button, 
                you will see the state Web page return to a previous button value, representing the previous 
                button you pressed. However, you will see that the time within the Web page continues to be 
                incremented.
            </p>
            <p>To see the effect of this logic, do the following.</p>

            <ol>
                <li>Press <b>1</b>. See the panel refresh.</li>
                <li>Press <b>3</b>. See the panel refresh.</li>
                <li>Press <b>2</b>. See the panel refresh.</li>
                <li>Press the browser's Back button. Note that the panel is refreshed with previous
                    data, but the date and time are updated to current values.</li>
            </ol>
            <asp:UpdatePanel ID="UpdatePanel1" >
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel  CssClass="box" ID="Content" Height="40px">
                        Date and Time:
                        <%= DateTime.Now.ToLongTimeString() %>
                        <br />
                        Page's refresh state:
                        <asp:Label  ID="LabelHistoryData" />
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <p />
            <asp:Button  ID="Button1" Text="Key 1" OnClick="ButtonClick" />
            <asp:Button  ID="Button2" Text="Key 2" OnClick="ButtonClick" />
            <asp:Button  ID="Button3" Text="Key 3" OnClick="ButtonClick" />
        </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    private static String key = "s";

    // Handle the Navigate event
    public void OnNavigateHistory(object sender, HistoryEventArgs e) {
        LabelHistoryData.Text = Server.HtmlEncode(e.State[key]);
    }

    // On button click, handle the event and set a history point. 
    public void ButtonClick(object sender, EventArgs e) {
        LabelHistoryData.Text = ((Button)sender).Text;
        ScriptManager.GetCurrent(this).AddHistoryPoint(key, LabelHistoryData.Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code>event.<br />
                    </li>
                </ol>
            </div>

            <p>
                In this example, three buttons outside the <code>UpdatePanel</code> control can
                trigger an asynchronous postback. You can see the results of the update through
                the date and time that renders in the Web page along with a value indicating the 
                button that triggered the partial refresh.</p>
            <p>
                When you press a button, the server-side <code>Click</code> event handler for the button 
                stores data and uses the data as a history point. If you click the browser's Back button, 
                you will see the state Web page return to a previous button value, representing the previous 
                button you pressed. However, you will see that the time within the Web page continues to be 
                incremented.
            </p>
            <p>To see the effect of this logic, do the following.</p>

            <ol>
                <li>Press <b>1</b>. See the panel refresh.</li>
                <li>Press <b>3</b>. See the panel refresh.</li>
                <li>Press <b>2</b>. See the panel refresh.</li>
                <li>Press the browser's Back button. Note that the panel is refreshed with previous
                    data, but the date and time are updated to current values.</li>
            </ol>
            <asp:UpdatePanel ID="UpdatePanel1" >
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel  CssClass="box" ID="Content" Height="40px">
                        Date and Time:
                        <%= DateTime.Now.ToLongTimeString() %>
                        <br />
                        Page's refresh state:
                        <asp:Label  ID="LabelHistoryData" />
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <p />
            <asp:Button  ID="Button1" Text="Key 1" OnClick="ButtonClick" />
            <asp:Button  ID="Button2" Text="Key 2" OnClick="ButtonClick" />
            <asp:Button  ID="Button3" Text="Key 3" OnClick="ButtonClick" />
        </div>
    </form>
</body>
</html>

Behandeln von Anforderungen an den Server

Wenn der Serverzustand in einer Anforderung erkannt wird, wird das Navigate-Ereignis ausgelöst. Dies wird wie ein asynchrones Postback zum Server angezeigt. Wenn Sie feststellen müssen, ob das Postback aufgrund der Navigation oder zu einem anderen Zweck erfolgt ist, können Sie die IsNavigating-Eigenschaft lesen. Wenn diese Eigenschaft auf true festgelegt ist, war der Ursprung des Postbacks ein Navigationsaufruf.

Im folgenden Beispiel wird das Wizard-Serversteuerelement in einem UpdatePanel-Steuerelement dargestellt. Dies bewirkt, dass das Wizard-Steuerelement asynchrone Postbacks ausführt, wenn Benutzer im Assistenten navigieren. Im Beispiel fügt Code Verlaufspunkte hinzu, wenn in den Schritten im Assistenten navigiert wird.

Führen Sie ein Beispiel für dieses Feature aus.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    Private Shared stepKey As String = "s"

    Protected Sub OnNavigateHistory(ByVal sender As Object, ByVal args As HistoryEventArgs)
        Dim stateString As String = args.State(stepKey)
        Dim [step] As Integer = If(stateString IsNot Nothing, Integer.Parse(stateString), 0)
        MachineConfiguratorWizard.ActiveStepIndex = [step]
    End Sub

    Protected Sub OnActiveStepChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Not ScriptManager1.IsNavigating AndAlso IsPostBack Then
            Dim index As Integer = MachineConfiguratorWizard.ActiveStepIndex
            ScriptManager1.AddHistoryPoint(stepKey, index.ToString(), "Step " + (index + 1).ToString())
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <div>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code> event.
                    </li>
                    <li>Protecting the history code with <code>IsNavigating</code>
                    </li>
                </ol>
            </div>
        <p>
        In this example, the <code>Wizard</code> server control provides it's own navigation, but 
        as each step is selected a history point is added. In order to do this, a history point is only added if the page is not being refreshed beacuse of a history point.</p>
            <asp:UpdatePanel  ID="WizardPanel">
                <ContentTemplate>
                    <asp:Wizard ID="MachineConfiguratorWizard"  ActiveStepIndex="0" BackColor="#dddddd"
                        BorderWidth="10" CellPadding="10" CellSpacing="10" Height="200px" Width="700px"
                        FinishPreviousButtonText="<" StartNextButtonText=">" StepNextButtonText=">" StepPreviousButtonText="<"
                        FinishCompleteButtonText="<|>" OnActiveStepChanged="OnActiveStepChanged">
                        <WizardSteps>
                            <asp:WizardStep ID="Step1"  Title="Step 1">
                                <h2>
                                    STEP 1</h2>
                                <br />
                            </asp:WizardStep>
                            <asp:WizardStep ID="Step2"  Title="Step 2">
                                <h2>
                                    STEP 2</h2>
                                <br />
                            </asp:WizardStep>
                            <asp:WizardStep ID="Step3"  Title="Step 3">
                                <h2>
                                    STEP 3</h2>
                                <br />
                            </asp:WizardStep>
                        </WizardSteps>
                        <StepStyle Font-Names="tahoma" Font-Size="Smaller" VerticalAlign="Top" />
                        <SideBarStyle Font-Size="Small" VerticalAlign="Top" BackColor="#FFFFC0" Font-Names="tahoma" />
                        <FinishPreviousButtonStyle BackColor="White" BorderColor="Black" BorderWidth="3px"
                            Font-Names="Tahoma" Font-Size="Medium" />
                        <NavigationButtonStyle BackColor="White" BorderColor="Black" BorderStyle="Solid"
                            BorderWidth="3px" Font-Names="Tahoma" Font-Size="Medium" />
                        <FinishCompleteButtonStyle Font-Names="Tahoma" Font-Size="Medium" />
                    </asp:Wizard>
                </ContentTemplate>
            </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    private static readonly string stepKey = "s";

    protected void OnNavigateHistory(object sender, HistoryEventArgs args)
    {
        string stateString = args.State[stepKey];
        int step = (stateString != null) ? int.Parse(stateString) : 0;
        MachineConfiguratorWizard.ActiveStepIndex = step;
    }

    protected void OnActiveStepChanged(object sender, EventArgs e)
    {
        if (!ScriptManager1.IsNavigating && IsPostBack) {
            int index = MachineConfiguratorWizard.ActiveStepIndex;
            ScriptManager1.AddHistoryPoint(stepKey, index.ToString(), "Step " + (index+1).ToString());
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <div>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code> event.
                    </li>
                    <li>Protecting the history code with <code>IsNavigating</code>
                    </li>
                </ol>
            </div>
        <p>
        In this example, the <code>Wizard</code> server control provides it's own navigation, but 
        as each step is selected a history point is added. In order to do this, a history point is only added if the page is not being refreshed beacuse of a history point.</p>
            <asp:UpdatePanel  ID="WizardPanel">
                <ContentTemplate>
                    <asp:Wizard ID="MachineConfiguratorWizard"  ActiveStepIndex="0" BackColor="#dddddd"
                        BorderWidth="10" CellPadding="10" CellSpacing="10" Height="200px" Width="700px"
                        FinishPreviousButtonText="<" StartNextButtonText=">" StepNextButtonText=">" StepPreviousButtonText="<"
                        FinishCompleteButtonText="<|>" OnActiveStepChanged="OnActiveStepChanged">
                        <WizardSteps>
                            <asp:WizardStep ID="Step1"  Title="Step 1">
                                <h2>
                                    STEP 1</h2>
                                <br />
                            </asp:WizardStep>
                            <asp:WizardStep ID="Step2"  Title="Step 2">
                                <h2>
                                    STEP 2</h2>
                                <br />
                            </asp:WizardStep>
                            <asp:WizardStep ID="Step3"  Title="Step 3">
                                <h2>
                                    STEP 3</h2>
                                <br />
                            </asp:WizardStep>
                        </WizardSteps>
                        <StepStyle Font-Names="tahoma" Font-Size="Smaller" VerticalAlign="Top" />
                        <SideBarStyle Font-Size="Small" VerticalAlign="Top" BackColor="#FFFFC0" Font-Names="tahoma" />
                        <FinishPreviousButtonStyle BackColor="White" BorderColor="Black" BorderWidth="3px"
                            Font-Names="Tahoma" Font-Size="Medium" />
                        <NavigationButtonStyle BackColor="White" BorderColor="Black" BorderStyle="Solid"
                            BorderWidth="3px" Font-Names="Tahoma" Font-Size="Medium" />
                        <FinishCompleteButtonStyle Font-Names="Tahoma" Font-Size="Medium" />
                    </asp:Wizard>
                </ContentTemplate>
            </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

URLs und Serververlaufszustand

Wenn Sie einen Verlaufspunkt erstellen, legen Sie fest, welche Informationen für den Verlaufspunktzustand gespeichert werden sollen. Sie müssen mindestens ein Schlüssel-Wert-Paar verwenden, das den Zustand zur Anwendung identifiziert. Sie können weitere Schlüssel-Wert-Paare speichern. Da diese Informationen jedoch in der URL gespeichert werden und für die URL eine (vom Browser festgelegte) Größenbeschränkung besteht, speichern Sie nur die Informationen, die Sie für die erneute Erstellung des Zustands benötigen.

In der URL können die Serverzustandsinformationen je nach der Einstellung, die Sie für die EnableHistory-Eigenschaft des ScriptManager-Serversteuerelements festlegen, in einem gehashten oder nicht gehashten Format vorliegen.

Der Verlaufszustand wird in der URL durch das #-Zeichen (Fragmenttrennzeichen) getrennt. Zustandsinformationen folgen auf das"&&"-Trennzeichen, wie im folgenden Beispiel dargestellt:

Default.aspx#&&s=Key+2

Wenn Sie EnableHistory auf true festlegen, wird das Verlaufszustandsfragment verschlüsselt, bevor es an die URL der Webseite angehängt wird. Dadurch wird die Sicherheit optimiert, indem es einem Angreifer erschwert wird, die Zustandsdaten zu ändern. Auch wenn die Informationen verschlüsselt sind, speichern Sie dennoch keine vertraulichen Daten im Zustandsfeld.

Beachten Sie, dass durch das Hashing von Zustandsinformationen umfangreiche URLs erstellt werden, die Informationen enthalten, die für den Benutzer nicht relevant sind.

Hinzufügen von Titeln zu Verlaufspunkten

Einträge im Verlaufsstapel des Browsers werden in der Regel durch den Titel der Seite für diesen Eintrag identifiziert. Ein Beispiel dafür finden Sie in der Verlaufsliste des Browsers, in der die Titel kürzlich aufgerufener Seiten angezeigt werden. (Sie können die Liste normalerweise über eine Dropdownliste im URL-Adressfeld sehen.)

Wenn Sie einen Verlaufspunkt in Ihrer Anwendung erstellen, wird der Eintrag standardmäßig mit dem Titel der Seite identifiziert. Wenn Sie mehrere Verlaufspunkte von derselben Seite hinzufügen, haben alle Einträge standardmäßig denselben Titel.

Sie können jedoch sinnvolle Titel für einzelne Verlaufseinträge angeben. In Servercode können Sie Titelinformationen aufnehmen, wenn Sie einen Verlaufspunkt durch Aufrufen der AddHistoryPoint-Methode erstellen.

Im folgenden Beispiel wird eine Variation zum vorherigen Beispiel dargestellt, indem einen Titeleintrag aufgenommen wird.

Führen Sie ein Beispiel für dieses Feature aus.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Collections.Generic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    Private Shared key As String = "s"

    ' Handle the Navigate event
    Public Sub OnNavigateHistory(ByVal sender As Object, ByVal e As HistoryEventArgs)
        LabelHistoryData.Text = Server.HtmlEncode(e.State(key))
    End Sub

    ' On button click, handle the event and set a history point. 
    Public Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        LabelHistoryData.Text = CType(sender, Button).Text
        ScriptManager.GetCurrent(Me).AddHistoryPoint(key, LabelHistoryData.Text, "Entry: " + LabelHistoryData.Text)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code>event.<br />
                    </li>
                </ol>
            </div>

            <p>
                In this example, three buttons outside the <code>UpdatePanel</code> control can
                trigger an asynchronous postback. You can see the results of the update through
                the date and time that renders in the Web page along with a value indicating the 
                button that triggered the partial refresh.</p>
            <p>
                When you press a button, the server-side <code>Click</code> event handler for the button 
                stores data and uses the data as a history point. If you click the browser's Back button, 
                you will see the state Web page return to a previous button value, representing the previous 
                button you pressed. However, you will see that the time within the Web page continues to be 
                incremented.
            </p>
            <p>To see the effect of this logic, do the following.</p>

            <ol>
                <li>Press <b>1</b>. See the panel refresh.</li>
                <li>Press <b>3</b>. See the panel refresh.</li>
                <li>Press <b>2</b>. See the panel refresh.</li>
                <li>Press the browser's Back button. Note that the panel is refreshed with previous
                    data, but the date and time are updated to current values.</li>
            </ol>
            <asp:UpdatePanel ID="UpdatePanel1" >
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel  CssClass="box" ID="Content" Height="40px">
                        Date and Time:
                        <%= DateTime.Now.ToLongTimeString() %>
                        <br />
                        Page's refresh state:
                        <asp:Label  ID="LabelHistoryData" />
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <p />
            <asp:Button  ID="Button1" Text="Key 1" OnClick="ButtonClick" />
            <asp:Button  ID="Button2" Text="Key 2" OnClick="ButtonClick" />
            <asp:Button  ID="Button3" Text="Key 3" OnClick="ButtonClick" />
        </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >
    private static String key = "s";

    // Handle the Navigate event
    public void OnNavigateHistory(object sender, HistoryEventArgs e) {
        LabelHistoryData.Text = Server.HtmlEncode(e.State[key]);
    }

    // On button click, handle the event and set a history point. 
    public void ButtonClick(object sender, EventArgs e) {
        LabelHistoryData.Text = ((Button)sender).Text;
        ScriptManager.GetCurrent(this).AddHistoryPoint(key, LabelHistoryData.Text, "Entry: " + LabelHistoryData.Text);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" >
    <title>Microsoft ASP.NET 3.5 Extensions: Managing History</title>
    <link href="../../include/qsstyle.css" type="text/css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" >
        <div>
            <asp:ScriptManager  ID="ScriptManager1" OnNavigate="OnNavigateHistory" 
                EnableHistory="true" EnableSecureHistoryState="false" />
            <h2>
                Microsoft ASP.NET 3.5 Extensions: Adding Server-side Browser History Points and Title Entries</h2>
            <p/>

            <div id="Div1" class="new">
                <p>This sample shows:</p>
                <ol>
                    <li>How to use the <code>ScriptManager</code> control to set a history point and add titles.</li>
                    <li>The <code>ScriptManager</code> control, the <code>EnableHistory</code> and 
                    <code>EnableSecureHistoryState</code> properties and 
                    the <code>OnNavigate</code> property to handle the <code>navigate</code>event.<br />
                    </li>
                </ol>
            </div>

            <p>
                In this example, three buttons outside the <code>UpdatePanel</code> control can
                trigger an asynchronous postback. You can see the results of the update through
                the date and time that renders in the Web page along with a value indicating the 
                button that triggered the partial refresh.</p>
            <p>
                When you press a button, the server-side <code>Click</code> event handler for the button 
                stores data and uses the data as a history point. If you click the browser's Back button, 
                you will see the state Web page return to a previous button value, representing the previous 
                button you pressed. However, you will see that the time within the Web page continues to be 
                incremented.
            </p>
            <p>To see the effect of this logic, do the following.</p>

            <ol>
                <li>Press <b>1</b>. See the panel refresh.</li>
                <li>Press <b>3</b>. See the panel refresh.</li>
                <li>Press <b>2</b>. See the panel refresh.</li>
                <li>Press the browser's Back button. Note that the panel is refreshed with previous
                    data, but the date and time are updated to current values.</li>
                <li>Press the browser's "Recent Pages" drop down menu and review the history entries and their titles.</li>
            </ol>
            <asp:UpdatePanel ID="UpdatePanel1" >
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                    <asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel  CssClass="box" ID="Content" Height="40px">
                        Date and Time:
                        <%= DateTime.Now.ToLongTimeString() %>
                        <br />
                        Page's refresh state:
                        <asp:Label  ID="LabelHistoryData" />
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>
            <p />
            <asp:Button  ID="Button1" Text="Key 1" OnClick="ButtonClick" />
            <asp:Button  ID="Button2" Text="Key 2" OnClick="ButtonClick" />
            <asp:Button  ID="Button3" Text="Key 3" OnClick="ButtonClick" />
        </div>
    </form>
</body>
</html>

Siehe auch

Weitere Ressourcen

Verwalten des Browserverlaufs