<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>ASP.NET</title>
        <link>http://geekswithblogs.net/sankarsan/category/9515.aspx</link>
        <description>ASP.NET</description>
        <language>en-US</language>
        <copyright>sankarsan</copyright>
        <managingEditor>sankarsan_b@yahoo.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Cross Page Postback in ASP.NET</title>
            <link>http://geekswithblogs.net/sankarsan/archive/2009/01/24/cross-page-postback-in-asp.net.aspx</link>
            <description>&lt;p&gt;Normally we use the term postback when an ASP.NET page submits it's content back to that page itself.But there can be situation when a page needs to submit it's content to a different target page.This is known as cross page postback.In this post we will discuss about how to handle and implement cross page postback scenario.&lt;/p&gt;
&lt;p&gt;Now to make a page postback to another page we have set the &lt;font color="#0000ff"&gt;PostBackUrl&lt;/font&gt; property as shown below:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;&amp;lt;asp:Button ID="Button2" runat="server" Text="CrossPagePostback" PostBackUrl="~/TargetForm.aspx" /&amp;gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;After the content is submitted to the target url we need to retrieve the control values and properties of the page.To do this we have use the &lt;font color="#0000ff"&gt;PreviousPage&lt;/font&gt; property of the &lt;font color="#0000ff"&gt;Page&lt;/font&gt; class as shown below.The PreviousPage maintains a reference to the instance of the source page.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;Label1.Text = "Crosspage Postback with value :" + (Page.PreviousPage.FindControl("TextBox1") as TextBox).Text;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Suppose we have a public property defined in the source page as shown below:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;public string ScreenID { get; set; }&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;If we want to access this property in the target page then we need to specify the type of source page in the &lt;font color="#0000ff"&gt;@PreviousPageType&lt;/font&gt; directive of the target page:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;&amp;lt;%@ PreviousPageType VirtualPath="~/TargetForm.aspx"  %&amp;gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Then we can access the property in target page as follows:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;Label1.Text = PreviousPage.ScreenID;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Like the &lt;font color="#0000ff"&gt;IsPostBack&lt;/font&gt; property used to identify as post back we can identify a cross page postback using the &lt;font color="#0000ff"&gt;IsCrossPagePostBack&lt;/font&gt; as shown below:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;if(Page.PreviousPage.IsCrossPagePostBack) &lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;{&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#800000"&gt;}&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Now let us examine the HTML source of the page to understand how the cross page postback is initiated from the browser.From the click event of the button a JavaScript function called &lt;font color="#0000ff"&gt;WebForm_DoPostBackWithOptions&lt;/font&gt; is invoked as shown below:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#008000"&gt;&amp;lt;input type="submit" name="Button2" value="CrossPagePostback" &lt;strong&gt;onclick="javascript:WebForm_DoPostBackWithOptions&lt;/strong&gt;(new WebForm_PostBackOptions(&amp;amp;quot;Button2&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, false, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;TargetForm.aspx&amp;amp;quot;, false, false))" id="Button2" /&amp;gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;This function accepts a &lt;font color="#0000ff"&gt;WebForm_PostBackOptions&lt;/font&gt; function as parameter.The WebForm_PostBackOption accepts the following values as argument &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;"Button2"  - Id of the control trigerring the event &lt;/li&gt;
    &lt;li&gt;"" &lt;/li&gt;
    &lt;li&gt;false &lt;/li&gt;
    &lt;li&gt;"" &lt;/li&gt;
    &lt;li&gt;"TargetForm.aspx" - Target page url. &lt;/li&gt;
    &lt;li&gt;false &lt;/li&gt;
    &lt;li&gt;false &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But where is the code for this function.Now we look at the following extra lines of code emitted:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#008000"&gt;&amp;lt;script src="/WebResource.axd?d=ZhZ_K2lwE0H8HnJR9LeB3Q2&amp;amp;amp;t=633394762369896337" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I typed the url of the script using it's src attribute in address bar and downloaded the axd file.On opening the file in notepad I found the definitions of the two functions mentioned above.&lt;/p&gt;
&lt;strong&gt;&lt;em&gt;&lt;font color="#008000"&gt;//This function creates the post back options object&lt;/font&gt;&lt;/em&gt; &lt;br /&gt;
&lt;/strong&gt;
&lt;p&gt;&lt;em&gt;&lt;font color="#008000"&gt;function WebForm_PostBackOptions(eventTarget, eventArgument, validation, validationGroup, actionUrl, trackFocus, clientSubmit) { &lt;br /&gt;
&lt;strong&gt;    this.eventTarget = eventTarget; //id of the control trigerring postback&lt;/strong&gt; &lt;br /&gt;
    this.eventArgument = eventArgument;  &lt;br /&gt;
    this.validation = validation; &lt;br /&gt;
    this.validationGroup = validationGroup; &lt;br /&gt;
&lt;strong&gt;    this.actionUrl = actionUrl; //URL of the target page&lt;/strong&gt; &lt;br /&gt;
    this.trackFocus = trackFocus; &lt;br /&gt;
    this.clientSubmit = clientSubmit; &lt;br /&gt;
}&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;font color="#008000"&gt;//This function sets the target url&lt;/font&gt;&lt;/em&gt; &lt;br /&gt;
&lt;/strong&gt;&lt;font color="#008000"&gt;&lt;em&gt;function WebForm_DoPostBackWithOptions(options) { &lt;br /&gt;
    var validationResult = true; &lt;br /&gt;
    if (options.validation) { &lt;br /&gt;
        if (typeof(Page_ClientValidate) == 'function') { &lt;br /&gt;
            validationResult = Page_ClientValidate(options.validationGroup); &lt;br /&gt;
        } &lt;br /&gt;
    } &lt;br /&gt;
    if (validationResult) { &lt;br /&gt;
        if ((typeof(options.actionUrl) != "undefined") &amp;amp;&amp;amp; (options.actionUrl != null) &amp;amp;&amp;amp; (options.actionUrl.length &amp;gt; 0)) { &lt;br /&gt;
&lt;strong&gt;            theForm.action = options.actionUrl; // form action is set to target page url.&lt;/strong&gt; &lt;br /&gt;
        } &lt;br /&gt;
        if (options.trackFocus) { &lt;br /&gt;
            var lastFocus = theForm.elements["__LASTFOCUS"]; &lt;br /&gt;
            if ((typeof(lastFocus) != "undefined") &amp;amp;&amp;amp; (lastFocus != null)) { &lt;br /&gt;
                if (typeof(document.activeElement) == "undefined") { &lt;br /&gt;
                    lastFocus.value = options.eventTarget; &lt;br /&gt;
                } &lt;br /&gt;
                else { &lt;br /&gt;
                    var active = document.activeElement; &lt;br /&gt;
                    if ((typeof(active) != "undefined") &amp;amp;&amp;amp; (active != null)) { &lt;br /&gt;
                        if ((typeof(active.id) != "undefined") &amp;amp;&amp;amp; (active.id != null) &amp;amp;&amp;amp; (active.id.length &amp;gt; 0)) { &lt;br /&gt;
                            lastFocus.value = active.id; &lt;br /&gt;
                        } &lt;br /&gt;
                        else if (typeof(active.name) != "undefined") { &lt;br /&gt;
                            lastFocus.value = active.name; &lt;br /&gt;
                        } &lt;br /&gt;
                    } &lt;br /&gt;
                } &lt;br /&gt;
            } &lt;br /&gt;
        } &lt;br /&gt;
    } &lt;br /&gt;
    if (options.clientSubmit) { &lt;br /&gt;
        __doPostBack(options.eventTarget, options.eventArgument); &lt;br /&gt;
    } &lt;br /&gt;
}&lt;/em&gt;&lt;/font&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/sankarsan/aggbug/128951.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>sankarsan</dc:creator>
            <guid>http://geekswithblogs.net/sankarsan/archive/2009/01/24/cross-page-postback-in-asp.net.aspx</guid>
            <pubDate>Sat, 24 Jan 2009 17:47:35 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/sankarsan/comments/128951.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/sankarsan/archive/2009/01/24/cross-page-postback-in-asp.net.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/sankarsan/comments/commentRss/128951.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
