JavaScript Editor jscript editor     Web designer 



Main Page

If your application redirects (navigates) from one ASP.NET Web page to another, you will frequently want to pass information from the source page to the target page. For example, you might have a page where users can select items to purchase. When users submit the page, you want to call another page that can process the information that the user has entered.

You can pass information between pages in various ways, some of which depend on how the redirection occurs. Options include the following:

Getting Public Property Values from the Source Page

If you are designing the source page specifically for sharing information with target pages, and both pages are ASP.NET Web pages, in the source page you can add public properties that expose information you want to share between pages. You can then read the values of the properties in the target pages.

NoteNote

You can read source page properties in the target page only if both pages are in the same Web application.

To get public property values from the source page

  1. On the source page, create one or more public properties.

    The following code example shows a property named CurrentCity that exposes the value of a TextBox control named textCity.

    Visual BasicВ CopyCode imageCopy Code
    Public ReadOnly Property CurrentCity() As String
        Get
            Return textCity.Text
        End Get
    End Property

    C#В CopyCode imageCopy Code
    public String CurrentCity
    {
        get
        {
            return textCity.Text;
        }
    }
    NoteNote

    Properties on the source page that are created primarily to expose values for cross-page posting are usually read-only properties. Although the source page can contain public read/write properties, setting a source page property from the target page property generally has no purpose, because the value will not be persisted.

  2. On the target page, add an @ PreviousPageType page directive that points to the source page.

    The following code example shows a PreviousPageType directive that references a source page named SourcePage.aspx.

    В CopyCode imageCopy Code
    <%@В PreviousPageType VirtualPath="~/SourcePage.aspx" %> 

    The PreviousPageType directive causes the page's PreviousPage property to be typed to the source page class.

  3. In target page code, use strongly typed members of the PreviousPage property to read the source code properties.

    The following code example reads the value of the CurrentCity property that is defined in the source page.

    Visual BasicВ CopyCode imageCopy Code
    Label1.Text = PreviousPage.CurrentCity

    C#В CopyCode imageCopy Code
    Label1.Text = PreviousPage.CurrentCity;

Getting Control Information from the Source Page in the Same Application

If the source and target pages are both ASP.NET Web pages and in the same Web application, you can read the values of controls on the source page while in the target page. You might use this strategy if the source page does not expose public properties containing the information you need.

To get the values of controls from the source page in the same application

  • On the target page, get a reference to the source page by using the target page's PreviousPage property, and then call the FindControl method to get a reference to the control you want.

    The following code example gets the value of the source page's TextBox1 control and displays it in the control named Label1:

    Visual BasicВ CopyCode imageCopy Code
    If Not Page.PreviousPage Is Nothing Then
        Dim SourceTextBox As TextBox
        SourceTextBox = CType(PreviousPage.FindControl("TextBox1"), _
            TextBox)
        If Not SourceTextBox Is Nothing Then
            Label1.Text = SourceTextBox.Text
        End If
    End If

    C#В CopyCode imageCopy Code
    if (Page.PreviousPage != null)
    {
        TextBox SourceTextBox = 
            (TextBox)Page.PreviousPage.FindControl("TextBox1");
        if (SourceTextBox != null)
        {
            Label1.Text = SourceTextBox.Text;
        }
    }
    NoteNote

    The FindControl method finds controls in the current naming container. If the control you are looking for is inside another control (typically, inside a template), you must first get a reference to the container, and then search the container to find the control that you want to get.

Getting Post Information from the Source Page in Another Application

If the source and target pages are not in the same Web application, you can read the posted values of the source page in the target page. This technique also works if the target page is an ASP.NET Web page but the source page is not. Note that you can get only the post values; you cannot read the values of arbitrary controls on the page.

To get the values of controls from the source page in another application

  • On the target page, read the Form collection, which returns a dictionary of name/value pairs, one pair for each posted value.

    The following code example displays the ID and value of every posted control in the source page and displays the posted values in a label named Label1.

    NoteNote

    Post information from an ASP.NET Web pages includes the values of hidden fields, such as __VIEWSTATE, __EVENTTARGET, and __EVENTARGUMENT, which are used for internal processing in the page. The following code example excludes the values of posted fields that are named with a leading double underscore (__).

    Visual BasicВ CopyCode imageCopy Code
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
            Handles Me.Load
        Dim displayValues As New StringBuilder()
        Dim postedValues As NameValueCollection = Request.Form
        Dim nextKey As String
        For i As Integer = 0 To postedValues.AllKeys.Length - 1
            nextKey = postedValues.AllKeys(i)
            If nextKey.Substring(0, 2) <> "__" Then
                displayValues.Append("<br>")
                displayValues.Append(nextKey)
                displayValues.Append(" = ")
                displayValues.Append(postedValues(i))
            End If
        Next
        Label1.Text = displayValues.ToString()
    End Sub

    C#В CopyCode imageCopy Code
    void Page_Load(object sender, EventArgs e)
    {
        System.Text.StringBuilder displayValues = 
            new System.Text.StringBuilder();
        System.Collections.Specialized.NameValueCollection 
            postedValues = Request.Form;
        String nextKey;
        for(int i = 0; i < postedValues.AllKeys.Length - 1; i++)
        {
            nextKey = postedValues.AllKeys[i];
            if(nextKey.Substring(0, 2) != "__")
            {
                displayValues.Append("<br>");
                displayValues.Append(nextKey);
                displayValues.Append(" = ");
                displayValues.Append(postedValues[i]);
            }
        }
       Label1.Text = displayValues.ToString();
    }

See Also



JavaScript Editor jscript editor     Web designer