German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Cross Page Posting without PreviousPage in ASP.NET 2.0

Written by admin on Dec 25, 2006
Here we will discuss a very different method to access the Previous Page Control

Introducation:

Many of us know and use the PreviousPage method to access the Previous Page control Properties but here we are going to talk about very new way to perform this activity.

Explanation:

One of the potential drawbacks to using cross page posting in ASP.NET 2.0 is that the previous page is allocated and executed through its LoadComplete event so that the target page can look at all of the re-hydrated state of the controls in the previous page to retrieve the values posted by the client.

 

For some reason, it never occurred to me that you don't even need to use the PreviousPage property in the target page to retrieve the values, you can simply extract the name/value pairs from the POST body directly (just as you would in classic ASP or even straight HTML).

 

This Article is based on the question asked in ASP .NET Forum by Swati+Jain  the thread link is http://forums.asp.net/thread/1508099.aspx

 

now let us see what our pages look like;

 

This is "FirstPage.aspx"

 

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

<div>

<asp:ListBox ID="ListBox1" runat="server" Height="183px" Width="185px">asp:ListBox>

<br />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />div>

form>

 

Now "SecoundPage.aspx"

 

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

<div>

<asp:TextBox ID="txtStartDate" runat="server">asp:TextBox>

<br />

<asp:TextBox ID="txtEndDate" runat="server">asp:TextBox>

<br />

<asp:Button ID="btnGod" runat="server" Text="Go" PostBackUrl="~/firstPage.aspx" Width="61px" />

div>

form>

 

Let us see how does code behind look like for "FirstPage.aspx"

 

protected void Page_Load(object sender, EventArgs e)

{

string str1 = Request["txtStartDate"] + Request["txtEndDate"];

ListBox1.Items.Add(str1);

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("secoundPage.aspx");

}

 

Request["txtStartDate"]  is the way you can access the string Property of a TextBox.

 

The potential advantage of this technique is that the PreviousPage property of the Page class allocates and executes the prior page on demand, so if you never access the PreviousPage property, it will never allocate and execute the previous page at all! You don't have to worry about spending extra time processing a page that won't actually render, and you don't have to worry about instrumenting your source page's logic with conditionals using IsCrossPagePostBack to avoid executing code during the cross page post evaluation.

One caveat to this technique is that you must use the actual POST body name of the input element to extract the value, which may be different from the simple ID attribute of the control if it is embedded in another control. You can always just look at the rendered page to discover what this is, or you can use the ClientID attribute of any control to find it programmatically.

Hope it help.

Visitors/Readers Comments



Thank u Sir, for your article I tried above code First Item is getting replaced with Second item , i want that it should be added without replacing

Comments By:Swati Jain

at: 25/12/2006 23:32:21 UTC
Finaly i have solved this with codebehind for First Page as followsstring
str1 = "";
string[] items;
protected void Page_Load(object sender, EventArgs e) {
if (Session["items"] == null) {
Session["items"] = Request["txtStartDate"] + Request["txtEndDate"];
str1 = Request["txtStartDate"] + Request["txtEndDate"];
}
else {
Session["items"]+=Session["items"] + "," + Request["txtStartDate"] + Request["txtEndDate"];
str1 =Convert.ToString(Session["items"]);
}
items = str1.Split(',');
foreach (string item in items) {
ListBox1.Items.Add(item);
}

Comments By:Swati Jain

at: 25/12/2006 23:51:28 UTC
Good work Swati Jain. I was a bit away so could not get your first Message.

Comments By:Kamal

at: 26/12/2006 12:20:05 UTC
hi

Comments By:Naresh

at: 27/12/2007 01:07:39 UTC



Add your Comments

Name:
 
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)