ASP.NET 2.0's PostBackUrl attribute allows you to designate where a Web form and its data is sent when submitted.

Standard HTML forms allow you to post or send data to another page or application via the method attribute of the form element. In ASP.NET 1.x, Web pages utilise the postback mechanism where the page's data is submitted back to the page itself. ASP.NET 2.0 provides additional functionality by allowing a Web page to be submitted to another page. This week, I examine this new feature.

The old approach

I want to take a minute to cover the older HTML approach to gain perspective. The HTML form element contains the action attribute to designate what server side resource will handle the submitted data. The following code provides an example.

<html>
<head><title>Sample HTML form</title></head>
<body>
<form name="frmSample" method="post" action="target_url">
<input type="text" name="fullname" id="fullname" />
<input type="button" name="Submit" value="submit" />
</form>
</body></html>

The value entered in the text field (fullname) is submitted to the page or program specified in the form element's action attribute. For ASP.NET developers, standard HTML forms are seldom, if ever, used.

ASP.NET developers have plenty of options when faced with the task of passing values from page to page. This includes session variables, cookies, querystring variables, caching, and even Server.Transfer, but ASP.NET 2.0 adds another option.

ASP.NET 2.0 alternative

When designing ASP.NET 2.0, Microsoft recognised the need to easily cross post data between Web forms. With that in mind, the PostBackUrl attribute was added to the ASP.NET button control. It allows you to designate where the form and its data are sent when submitted (via the URL assigned to the PostBackUrl attribute). Basically, cross posting is a client side transfer that uses JavaScript behind the scenes.

The ASP.NET Web form in Listing A has two text fields (name and e-mail address) and a button to submit the data. The PostBackUrl attribute of the submit button is assigned to another Web form, so the data is sent to that page when the form is submitted. Note: The form is set up to post the data when it is submitted via the method attribute of the form element, but this is unnecessary since all cross postbacks utilise post by design.

Listing A

<%@ Page language="vb" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html><head>

<title>Cross Postback Example</title>

</head><body>

<form id="frmCrossPostback1" method="post" runat="server">

<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>

<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />

<asp:Label ID="lblE-mailAddress" runat="server" Text="E-mail:"></asp:Label>

<asp:TextBox ID="txtE-mailAddress" runat="server"></asp:TextBox><br />

<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="CrossPostback2.aspx" />

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

Working with previous pages

The IsPostBack property of an ASP.NET Page object is not triggered when it is loaded via a cross postback call. However, a new property called PreviousPage allows you to access and work with pages utilising cross postbacks.

When a cross page request occurs, the PreviousPage property of the current Page class holds a reference to the page that caused the postback. If the page is not the target of a cross-page posting, or if the pages are in different applications, the PreviousPage property is not initialised.

You can determine if a page is being loaded as a result of a cross postback by checking the PreviousPage object. A null value indicates a regular load, while a null value signals a cross postback. Also, the Page class contains a new method called IsCrossPagePostBack to specifically determine whether the page is loading as the result of a cross postback.

Once you determine that a cross postback has occurred, you can access controls on the calling page via the PreviousPage object's FindControl method. The code in Listing B is the second page in our example; it is called by the page in the previous listing.

Listing B

<%@ Page language="vb" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html><head>

<title>Cross Postback Example 2</title>

</head><body>

<script language="vb" runat="server">

Sub Page_Load()

If Not (Page.PreviousPage Is Nothing) Then

If Not (Page.IsCrossPagePostBack) Then
Response.Write("Name:" + CType(PreviousPage.FindControl("txtName"), TextBox).Text + "<BR>")
Response.Write("E-mail:" + CType(PreviousPage.FindControl("txtE-mailAddress"), TextBox).Text + "<BR>")

End If

End If

End Sub

</script></body></html>

The page determines if it is being called via a cross postback. If so, the values from the calling page are accessed using the FindControl method and converting the controls returned by this method to TextBox controls and displaying their Text properties.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Comments

1

ahmad - 23/06/07

i wanna be a good programmer

» Report offensive content

2

tut - 01/10/07

3

test - 08/12/08

''UserRoles.
Dim strUserRoles() As String = {"Admin", "Public"}

'List Item Roles.--- one Row
Dim ListItem1 As New List(Of String)
ListItem1.Add("Brand Portal Admin,Admin,Public")


Dim strListRoles(), strUserRoles1() As String
Dim i, j, k As Integer

For Each lstItem As String In ListItem1 'looping through each listItem

strListRoles = lstItem.Split(",") ' ToSplit listItems which contains morethan one role.

For i = 0 To strUserRoles.Length - 1 'looping through each User role of one particular item

strUserRoles1 = strUserRoles(i).Split(",")
Dim boolFlag As Boolean = False

For j = 0 To strListRoles.Length - 1 'looping through each List role of one particular item

For k = 0 To strUserRoles1.Length - 1 'looping through each User role of one particular item

If strListRoles(j).ToLower() = strUserRoles1(k).ToLower() Then
boolFlag = True 'Check if user role and list role are equal then set the flag to true.
End If
Next
Next

If boolFlag = True Then 'if flag is true add link.
' Add Link Here...
Response.Write("Test")
End If

Next
Next
End Sub

» Report offensive content

4

kumar - 11/12/08

Bujji, try to learn Sql Server Reorting Services following link are very good.


http://www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-1/

http://www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-2/

http://www.eggheadcafe.com/articles/20040823.asp

» Report offensive content

5

rbgreen - 03/02/09

Tried the simple example, when the cosspostback2.asp is loaded it executes the page_load twice. The first time the values are set, the second time they get cleared. I have attached my code...

CrossPostback1.aspx

<%@ Page language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html><head>
<title>Cross Postback Example</title>
</head><body>
<form id="frmCrossPostback1" method="post" runat="server">
<asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="lblEmailAddress" runat="server" Text="E-mail:"></asp:Label>
<asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" PostBackUrl="CrossPostback2.aspx" />
</form></body></html>

CrossPostback2.aspx

<%@ Page language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Cross Postback Example 2</title>
</head><body>
<form id="frmCrossPostback2" method="post" runat="server">

<script language="vb" runat="server">
Sub Page_Load()
If Not (Page.PreviousPage Is Nothing) Then 'Page load is being run 2 times...
If Not (Page.IsCrossPagePostBack) Then
name1.text = CType(PreviousPage.FindControl("txtName"), TextBox).Text
name2.text = CType(PreviousPage.FindControl("txtEmailAddress"), TextBox).Text
End If
End If
End Sub

<asp textbox id="name1" runat="server"></asp:textbox><br/>
<asp textbox id="name2" runat="server"></asp:textbox><br/>

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

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

5

rbgreen - 02/03/09

Tried the simple example, when the cosspostback2.asp is loaded it executes the page_load twice. The first time the values are ... more

4

kumar - 12/11/08

Bujji, try to learn Sql Server Reorting Services following link are very good. http://www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-1/ http://www.simple-talk.com/sql/learn-sql-server/beginning-sql-server-2005-reporting-services-part-2/ http://www.eggheadcafe.com/articles/20040823.asp ... more

3

test - 12/08/08

''UserRoles. Dim strUserRoles() As String = {"Admin", "Public"} ... more

Log in


Sign up | Forgot your password?

  • Staff Aussies to pay more for Win 7

    If you are looking to make some money in these troubled times, perhaps importing copies of Windows 7 could be for you. Read more »

    -- posted by Staff

  • Staff Firefox: Greens want it, 3.5rc2 not up to par

    This week's roundup looks at the situation surrounding a campaign to change Outlook HTML renderer, a Greens MP wants to install Firefox but is restricted and all the photos from the iPhone 3GS launch. Read more »

    -- posted by Staff

  • Chris Duckett Microsoft misses the Outlook point

    Ask designers which mail program is the bane of their existence, and you'll find that Outlook tops the list. The reason why the most popular email reader is also the most painful is simple: it uses Word to render HTML emails. Read more »

    -- posted by Chris Duckett

What's on?