12 out of 18 rated this helpful - Rate this topic

Button.PostBackUrl Property

Gets or sets the URL of the page to post to from the current page when the Button control is clicked.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
[ThemeableAttribute(false)]
public virtual string PostBackUrl { get; set; }
<asp:Button PostBackUrl="String" />

Property Value

Type: System.String
The URL of the Web page to post to from the current page when the Button control is clicked. The default value is an empty string (""), which causes the page to post back to itself.

Implements

IButtonControl.PostBackUrl

The PostBackUrl property allows you to perform a cross-page post using the Button control.

Note Note:

Only correctly specified paths work with this property. For example, relative paths (Test/default.aspx), absolute paths (https://localhost/WebApp/default.aspx) and virtual (~\Test\default.aspx) work correctly. Incorrectly formed paths such as "/Test/default.aspx" or "\Test\default.aspx" do not work. See ASP.NET Web Site Paths for a discussion on creating correct paths.

Set the PostBackUrl property to the URL of the Web page to post to when the Button control is clicked. For example, specifying Page2.aspx causes the page that contains the Button control to post to Page2.aspx. If you do not specify a value for the PostBackUrl property, the page posts back to itself.

Important note Important Note:

When performing a cross-page postback with controls with server-side validation, you should check that the page's IsValid property is true before processing the postback. In the case of a cross-page postback, the page to check is the PreviousPage. The following VB code shows how this is done:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.PreviousPage.IsValid Then
            ' Handle the post back
        Else
            Response.Write("Invalid")
        End If
End Sub

For more information on cross-page posting techniques, see Cross-Page Posting in ASP.NET Web Pages.

This property cannot be set by themes or style sheet themes. For more information, see ThemeableAttribute and ASP.NET Themes and Skins Overview.

The following code example demonstrates how to use the PostBackUrl property to perform a cross-page post. When the user clicks the Button control, the page posts the value entered in the text box to the target page specified by the PostBackUrl property. To run this sample, you must also create a file for the target page in the same directory as this code example. The code for target page is provided in the next example.

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">

    <h3>Button.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1" 
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:button id="Button1" 
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page" 
      postbackurl="Button.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:button>

  </form>
</body>
</html>


The following code example demonstrates how to use the Page.PreviousPage property to access a value that was posted from another page using the PostBackUrl property. This page gets the string that was posted from the previous page and displays it to the user. If you attempt to run this code example directly, you will get an error because the value of the text field will be null. Instead, use this code to create a target page and place the file in the same directory as the code for the previous example. The name of the file must correspond to the value specified for the PostBackUrl property in the previous example. When you run the code for the previous example, this page will execute automatically when the cross page post occurs.

Security note Security Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ page language="C#" %>

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

  void Page_Load (object sender, System.EventArgs e)
  {
    string text;

    // Get the value of TextBox1 from the page that 
    // posted to this page.
    text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;

    // Check for an empty string.
    if (text != "")
      PostedLabel.Text = "The string posted from the previous page is "
                         + text + ".";
    else
      PostedLabel.Text = "An empty string was posted from the previous page.";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.PostBackUrl Target Page Example</h3>

    <br />

    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </form>
</body>
</html>


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Needed: something like a "PostbackGroup"
Can be thought of as similar to "Validation Group".

Why?
This feature/"work around" does not understand the concept of multiple forms really. Why would the "receiving page" need to receive POSTed Form variables irrelevant to it?

These samples are "all ASP.Net" applications - in the real world, where APIs exist, you will need to exchange data with external applications. If the web app you try to exchange data with is "locked down" with specific variables for external apps to conform to it (aka a schema), what now?

You'd have to cobble up some javascript hacks to submit "plain old html form" or forms outside of the main asp.net form. If the sentence sounds convoluted, it is, and represents what one would have to do, unfortunately...

So, a "PostGroup" attribute that you can set for certain textboxes, etc, so that when its associated button is clicked, only those variables in the same "PostGroup" will be sent....please??
PostBackUrl, OnClick and PreviousPage: Have your cake and eat it too?

As has been mentioned on many forums in many posts/questions related to cross-page posting, people want to know how to get server code on the source page to run before posting to the target page, i.e. how to have a button with the PostBackUrl property set and still fire the handler for the OnClick event. One reason to want this is if there is some processing (calculation, database, etc.) required by the source page to determine value(s) required for the target page. The proper response is to clear the PostBackUrl property and perform a Server.Transfer([Target]) after the required processing in the OnClick event handler. One downside, perceivably, to this is that the URL displayed with the target page is still that of the source page. A Response.Redirect will avoid this issue but will leave the Page.PreviousPage property of the Target as null removing any benefit of first performing calculations on the Source as this communication channel is now inaccessible.


Here is some curious behavior that might be useful to be aware of. If the PostBackUrl property is set and there is an OnClick event handler, the OnClick event handler does not fire directly as the Source is posting directly to the Target. However, if the Target, in the Page_Load, examines the Page.PreviousPage property something interesting happens. At the point the Page.PreviousPage is evaluated flow shifts back to the Source starting with the Page_Load followed by the OnClick event handler in the same way it would have progressed if the Source had posted back to itself. It seems to continue with the standard lifecycle moving through the Page_Load of the MasterPage which has some interesting implications discussed further below. After the Source completes its lifecycle, flow then returns the Target at the point following where the PreviousPage was evaluated. So, if one needs to do additional processing on the Source to make data available to the Target it seems that this can be done in the OnClick event handler of a button that also has the PostBackUrl property set as long as the Target does not try to consume the information before referencing its Page.PreviousPage property. I don’t know that this approach would be considered best practice as the behavior seems kind of quirky.


On a related note, as mentioned above it seems that when the PreviousPage is referenced it causes the Source to go through its complete lifecycle including that of its MasterPage. If the Source/MasterPage are heavy during load, e.g. lots of validation, initialization or database transactions, it may be beneficial to wrap these processes in an if(){} block evaluating that Page.AppRelativeVirtualPath == Page.Request.AppRelativeCurrentExecutionPath. That is to say, the page that is currently running is that same page that kicked things off.