Prop Webcontrol Linkbutton Postbackurl
## ASP.NET LinkButton PostBackUrl Property
The `PostBackUrl` property of the `LinkButton` control is a powerful feature in ASP.NET Web Forms. It allows developers to perform **cross-page posting**, redirecting the user and submitting the form data to a different target page instead of posting back to the same page.
---
## Definition and Usage
By default, when a user clicks a `LinkButton` control, the page posts back to itself (self-postback).
The `PostBackUrl` property gets or sets the URL of the target page to which you want to post the form data when the `LinkButton` control is clicked. This is highly useful for multi-step wizards, search result redirection, or passing form inputs directly to another processing page.
---
## Syntax
```xml
```
### Property Values
| Value | Description |
| :--- | :--- |
| `string` | A string representing the URL of the target page for the postback. The default is an empty string (`""`), which causes the page to post back to itself. |
---
## Code Example
The following example demonstrates how to set the `PostBackUrl` property of a `LinkButton` control to submit user input to a target page named `TargetPage.aspx`.
### Source Page (`SourcePage.aspx`)
```xml
<%@ Page Language="C#" %>
Source Page - YouTip
```
### Target Page (`TargetPage.aspx.cs`)
To retrieve the values submitted from the source page, you can use the `PreviousPage` property in the code-behind of the target page:
```csharp
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TargetPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Check if the page was loaded via a cross-page postback
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
// Find the TextBox control from the source page
TextBox txtName = (TextBox)PreviousPage.FindControl("TextBox1");
if (txtName != null)
{
Response.Write("Hello, " + Server.HtmlEncode(txtName.Text) + "! Welcome to the target page.");
}
}
else
{
Response.Write("This page was not accessed via a cross-page postback.");
}
}
}
```
---
## Key Considerations & Best Practices
1. **Cross-Page Postback Detection**: Always check `PreviousPage != null` and `PreviousPage.IsCrossPagePostBack` in the target page before attempting to access controls from the source page to prevent `NullReferenceException` errors.
2. **Control Access**: Use `PreviousPage.FindControl("ControlID")` to access controls from the source page. If you want strongly-typed access to the source page's properties, add the `@PreviousPageType` directive to the target `.aspx` page:
```xml
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
```
3. **Validation**: If the source page contains ASP.NET validation controls (like `RequiredFieldValidator`), the cross-page postback will only occur if the source page validation passes (unless `CausesValidation="false"` is set on the `LinkButton`).
4. **Client-Side Behavior**: Under the hood, ASP.NET injects client-side JavaScript (`WebForm_DoPostBackWithOptions`) to modify the form's action attribute dynamically before submission when a `PostBackUrl` is specified. Ensure JavaScript is enabled in the client browser for this feature to work correctly.
YouTip