Click here to Skip to main content
Click here to Skip to main content

Performing Cross-page Posting in an Event Handler

, 5 May 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes an idea of the ASP.NET page transfer method that can be called like the Response.Redirect method and works as cross-page posting.

Table of Contents 

Introduction 

This article describes an idea of the ASP.NET page transfer method that can be called like the Response.Redirect method and works as cross-page posting.   

When I develop ASP.NET applications, I often use cross-page posting to transfer and to pass page data to another page. The advantage of cross-page posting is that the transferred page can access the previous page to retrieve page data. It reduces the cost of session management. So, I prefer to use cross-page posting.  

But, there is one thing I don't like about cross-page posting; it can't perform in an event handler. When we set value to the Button.PostBackUrl property to perform cross-page posting, unfortunately, the click event handler doesn't work. For this reason, if we want to transfer to another page after doing something in an event handler, we can't use cross-page posting.   

Thus, when we use cross-page posting, we have to use several implementation techniques. In some cases, we have to set the PostBackUrl property to perform cross-page posting. And in other cases, we have to implement an event handler to transfer to another page with the Response.Redirect method after doing something (although the HttpServerUtility.Transfer method can also be used, I don't want to use it). In addition, then, we must use session or query string to pass data to another page.

Consequently, the implementation of page transfer and data passing becomes complex.

To mitigate this complexity, I have come up with a mechanism that performs cross-page posting in an event handler.  It always allows us to transfer and to pass data to another page with cross-page posting.  

Concept

The concept of my idea is illustrated in Fig 1.

concept2.png

Fig 1. The concept of my idea.

The following are the main points of my idea:  

  1. Use an async postback to request server
  2. Return a cross-page posting script as the response of the async postback 
  3. Evaluate the returned script
  4. Retrieve previous page data with the PreviousPage property

1. Use an Async Postback to Request Server 

The ScriptManager.endRequest event will be raised when an async postback is completed. So, this event allows you to perform additional processes in client-side after completion of an async postback.  

2. Return a Cross-page Posting Script as the Response of the Async Postback 

The ClientScriptManager.GetPostBackEventReference method allows you to create a cross-page posting script in a server-side event handler. This method returns a cross-page posting script as string when you pass an instance of the PostBackOption class that is set up appropriately.

The ScriptManger.RegisterDataItem method can be used to return the created script as the response. This method allows you to store any data in the dataItems array. And, the dataItems array can be accessed in the endRequest event handler.

3. Evaluate the Returned Script 

In the endRequest event handler, you can retrieve the returned cross-page posting script via the dataItems array.  When you pass its script to the eval method, the cross-page posting is performed. 

4. Retrieve Previous Page Data with the PreviousPage Property

As with normal cross-page posting, previous page data can be accessed with the PreviousPage property from a next page.  Naturally, the PreviousPageType directive also works.

Example of Implementation

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptManager" runat="server" />
    <asp:UpdatePanel ID="updatePanel" runat="server">
        <ContentTemplate>
            <asp:Button ID="submitButton" runat="server" 
				Text="Submit" OnClick="Submit" />
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
Code 1. A part of Page1.aspx
using System;
using System.Text;
using System.Web;
using System.Web.UI;

namespace SampleCode
{
    public partial class Page1 : Page
    {
        public string OrderNo
        {
            get
            {
                return (string)ViewState["OrderNo"];
            }
            private set
            {
                ViewState["OrderNo"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            StringBuilder script = new StringBuilder();
            script.Append("var manager = 
		Sys.WebForms.PageRequestManager.getInstance();");
            script.Append("manager.add_endRequest(function(sender, args) {");
            script.Append("    if (args.get_error() == undefined) {");
            script.Append("        var dataItems = args.get_dataItems();");
            script.Append("        var item = dataItems['" + ClientID + "'];");
            script.Append("        if (item != undefined) {");
            script.Append("            eval(item);");
            script.Append("        }");
            script.Append("    }");
            script.Append("});");
            ClientScript.RegisterStartupScript(GetType(), 
			"doCrossPagePostBack", script.ToString(), true);
        }

        protected void Submit(object sender, EventArgs e)
        {
            Random r = new Random();
            OrderNo = String.Format("OD{0:00000000}", r.Next(0, 99999999));
            NavigateTo("~/Page2.aspx");
        }

        private void NavigateTo(string url)
        {
            ScriptManager manager = ScriptManager.GetCurrent(this);
            if (manager.IsInAsyncPostBack)
            {
                if (VirtualPathUtility.IsAbsolute(url) == false)
                {
                    url = VirtualPathUtility.ToAbsolute(url);
                }
                PostBackOptions o = new PostBackOptions(this, String.Empty, url, 
			false, false, false, true, false, String.Empty);
                manager.RegisterDataItem(this, 
			ClientScript.GetPostBackEventReference(o));
            }
        }
    }
}
Code 2. Page1.aspx.cs
<body>
    <form id="form1" runat="server">
    <asp:Label ID="orderNoLabel" runat="server" Text="Order No :" />
    <asp:Label ID="orderNoValueLabel" runat="server" />
    </form>
</body> 
Code 3. A part of Page2.aspx
using System;
using System.Web.UI;

namespace SampleCode
{
    public partial class Page2 : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {
                Page1 previousPage = (Page1)PreviousPage;
                this.orderNoValueLabel.Text = previousPage.OrderNo;
            }
        }
    }
}
Code 4. Page2.aspx.cs

History 

  • 2009-05-06: Published

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shou Takenaka
Software Developer PortalIsland Corporation
Japan Japan
No Biography provided

Comments and Discussions

 
GeneralA real world usage of ScriptManager.RegisterDataItem PinmemberMr. Truong Pham27-Mar-13 7:02 
GeneralSeems right that Button.PostbackURL doesn't expose handling event before posting to another page PinmemberMr. Truong Pham27-Mar-13 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 5 May 2009
Article Copyright 2009 by Shou Takenaka
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid