Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Dealing with PostBacks in ASP.NET

Posted by Ravi Varma Thumati on July 16, 2009

In Active Server Pages 3.0, developers had their pages post to other pages within the application. ASP.NET pages typically post back to themselves in order to process events (such as a button-click event).

For this reason, you must differentiate between posts for the first time a page is loaded by the end user and postbacks. A postback is just that—a posting back to the same page. The postback contains all the form information collected on the initial page for processing if required.

Because of all the postbacks that can occur with an ASP.NET page, you want to know whether a request is the first instance for a particular page or is a postback from the same page. You can make this check by using the IsPostBack property of the Page class, as shown in the following example:

VB

If Page.IsPostBack = True Then

‘ Do processing

End If

C#

if (Page.IsPostBack == true) {

// Do processing

}

In addition to checking against a True or False value, you can also find out if the request is not a postback in the following manner:

VB

If Not Page.IsPostBack Then

‘ Do processing

End If

C#

if (!Page.IsPostBack)

{

// Do processing

}

Cross-Page Posting

One common feature in ASP 3.0 that is difficult to achieve in ASP.NET 1.0/1.1 is the capability to do cross-page posting. Cross-page posting enables you to submit a form (say, Page1.aspx) and have this form and all the control values post themselves to another page (Page2.aspx).

Traditionally, any page created in ASP.NET 1.0/1.1 simply posted to itself, and you handled the control values within this page instance. You could differentiate between the page’s first request and any postbacks by using the Page.IsPostBack property, as shown here:

If Page.IsPostBack Then

‘deal with control values

End If

Even with this capability, many developers still wanted to be able to post to another page and deal with the first page’s control values on that page. This is now possible in ASP.NET 2.0, and it is quite a simple process.

For an example, create a page called Page1.aspx that contains a simple form. This page is shown below.

VB

<%@ Page Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

Protected Sub Button1_Click(ByVal sender As Object, _ByVal e As System.EventArgs)

Label1.Text = “Hello “ & TextBox1.Text & “<br />” & _

“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()

End Sub

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head runat=”server”>

<title>First Page</title>

</head>

<body>

<form id=”form1” runat=”server”>

Enter your name:<br />

<asp:Textbox ID=”TextBox1” Runat=”server”>

</asp:Textbox>

<p>

When do you want to fly?<br />

<asp:Calendar ID=”Calendar1” Runat=”server”></asp:Calendar></p>

<br />

<asp:Button ID=”Button1” Runat=”server” Text=”Submit page to itself”

OnClick=”Button1_Click” />

<asp:Button ID=”Button2” Runat=”server” Text=”Submit page to Page2.aspx”

PostBackUrl=”Page2.aspx” />

<p>

<asp:Label ID=”Label1” Runat=”server”></asp:Label></p>

</form>

</body>

</html>

C#

<%@ Page Language=”C#” %>

<script runat=”server”>

protected void Button1_Click (object sender, System.EventArgs e)

{

Label1.Text = “Hello “ + TextBox1.Text + “<br />” +

“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();

}

</script>

The code from Page1.aspx, as shown above, is quite interesting. First, two buttons are shown on the page. Both buttons submit the form, but each submits the form to a different location. The first button submits the form to itself. This is the behavior that has been the default for ASP.NET 1.0/1.1. In fact, nothing is different about Button1. It submits to Page1.aspx as a postback because of the use of the OnClick property in the button control. A Button1_Click event on Page1.aspx handles the values that are contained within the server controls on the page.

The second button, Button2, works quite differently. This button does not contain an OnClick event as the first button did. Instead, it uses the PostBackUrl property. This property takes a string value that point to the location of the file to which this page should post. In this case, it is Page2.aspx. This means that Page2.aspx now receives the postback and all the values contained in the Page1.aspx controls.Look at the code for Page2.aspx, shown in below example.

VB

<%@ Page Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Dim pp_Textbox1 As TextBox

Dim pp_Calendar1 As Calendar

pp_Textbox1 = CType(PreviousPage.FindControl(“Textbox1”), TextBox)

pp_Calendar1 = CType(PreviousPage.FindControl(“Calendar1”), Calendar)

Label1.Text = “Hello “ & pp_Textbox1.Text & “<br />” & _

“Date Selected: “ & pp_Calendar1.SelectedDate.ToShortDateString()

End Sub

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head runat=”server”>

<title>Second Page</title>

</head>

<body>

<form id=”form1” runat=”server”>

<asp:Label ID=”Label1” Runat=”server”></asp:Label>

</form>

</body>

</html>

C#

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void Page_Load(object sender, System.EventArgs e)

{

TextBox pp_Textbox1;

Calendar pp_Calendar1;

pp_Textbox1 = (TextBox)PreviousPage.FindControl(“Textbox1”);

pp_Calendar1 = (Calendar)PreviousPage.FindControl(“Calendar1”);

Label1.Text = “Hello “ + pp_Textbox1.Text + “<br />” + “Date Selected: “ +

pp_Calendar1.SelectedDate.ToShortDateString();

}

</script>

You have a couple of ways of getting at the values of the controls that are exposed from Page1.aspx from the second page. The first option is displayed in above example. To get at a particular control’s value that is carried over from the previous page, you simply create an instance of that control type and populate this instance using the FindControl method from the PreviousPage property. The String value assigned to the FindControl method is the Id value, which is used for the server control from the previous page. After this is assigned, you can work with the server control and its carried-over values just as if it had originally resided on the current page. You can see from the example that you can extract the Text and SelectedDate properties from the controls without any problem.

Another way of exposing the control values from the first page (Page1.aspx) is to create a Property for the control. This is shown in below.

VB

<%@ Page Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

Public ReadOnly Property pp_TextBox1() As TextBox

Get

Return TextBox1

End Get

End Property

Public ReadOnly Property pp_Calendar1() As Calendar

Get

Return Calendar1

End Get

End Property

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Label1.Text = “Hello “ & TextBox1.Text & “<br />” & _

“Date Selected: “ & Calendar1.SelectedDate.ToShortDateString()

End Sub

</script>

C#

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

public TextBox pp_TextBox1

{

get

{

return TextBox1;

}

}

public Calendar pp_Calendar1

{

get

{

return Calendar1;

}

}

protected void Button1_Click (object sender, System.EventArgs e)

{

Label1.Text = “Hello “ + TextBox1.Text + “<br />” +

“Date Selected: “ + Calendar1.SelectedDate.ToShortDateString();

}

</script>

Now that these properties are exposed on the posting page, the second page (Page2.aspx) can more easily work with the server control properties that are exposed from the first page. Below example shows you how Page2.aspx works with these exposed properties.

VB

<%@ Page Language=”VB” %>

<%@ PreviousPageType VirtualPath=”Page1.aspx” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _

“Date Selected: “ & _

PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()

End Sub

</script>

C#

<%@ Page Language=”C#” %>

<%@ PreviousPageType VirtualPath=”Page1.aspx” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void Page_Load(object sender, System.EventArgs e)

{

Label1.Text = “Hello “ + PreviousPage.pp_TextBox1.Text + “<br />” +

“Date Selected: “ +

PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();

}

</script>

In order to be able to work with the properties that Page1.aspx exposes, you have to strongly type the PreviousPage property to Page1.aspx. To do this, you use the PreviousPageType directive. This new directive allows you to specifically point to Page1.aspx with the use of the VirtualPath attribute.

When that is in place, notice that you can see the properties that Page1.aspx exposes through IntelliSense from the PreviousPage property. This is illustrated in Figure.

As you can see, working with cross-page posting is straightforward. Notice that, when you are crossposting from one page to another, you aren’t restricted to working only with the postback on the second page. In fact, you can still create methods on Page1.aspx that work with the postback before moving onto Page2.aspx. To do this, you simply add an OnClick event for the button in Page1.aspx and a method. You also assign a value for the PostBackUrl property. You can then work with the postback on Page1.aspx and then again on Page2.aspx.

postback intellisense

What happens if someone requests Page2.aspx before she works her way through Page1.aspx? It is actually quite easy to determine if the request is coming from Page1.aspx or if someone just hit Page2.aspx directly. You can work with the request through the use of the IsCrossPagePostBack property that is quite similar to the IsPostBack property from ASP.NET 1.0/1.1. The IsCrossPagePostBack property enables you to check whether the request is from Page1.aspx. Below shows an example of this;

VB

<%@ Page Language=”VB” %>

<%@ PreviousPageType VirtualPath=”Page1.aspx” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

If Page.IsCrossPagePostBack Then

Label1.Text = “Hello “ & PreviousPage.pp_Textbox1.Text & “<br />” & _“Date Selected: “ & _

PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString()

Else

Response.Redirect(“Page1.aspx”)

End If

End Sub

</script>

C#

<%@ Page Language=”C#” %>

<%@ PreviousPageType VirtualPath=”Page1.aspx” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void Page_Load(object sender, System.EventArgs e)

{

if (Page.IsCrossPagePostBack) {

Label1.Text = “Hello “ + PreviousPage.pp_Textbox1.Text + “<br />” +

“Date Selected: “ +

PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();

}

else

{

Response.Redirect(“Page1.aspx”);

}

}

</script>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: