Friday, September 16, 2005

Cross Page Postback Techniques : Comparing ASP.NET 1.X and 2.0

In an every day life of ASP.NET programmer there are number of occassions when he/she would want to postback a page to some other target page. And then from target page he/she would like to fetch information from source page (the page that triggered the postback). With ASP.Net 1.x this was only possible thrgouh Server.Transfer (that keeps the state of posted page). And then on the target one would always use Context.Handler and type cast it into the page that orginated this request (i.e.source page) .


Following is a code snippet that shows how calling server.transfer transfers state of source page to the target page.



Source page (WebForm1.aspx)

Server.Transfer("WebForm2.aspx"); // Transfer to target page URL



Target page (WebForm2.aspx)



// variable to hold webform1 instance

private WebForm1 previousPage;



// type casting it

if(Context.Handler != null && Context.Handler is WebForm1)

{

previousPage = (WebForm1) Context.Handler;

}



And then through previousPage reference one can access properties that were exposed on that page. Like if “SpookyValue” was some public proprty in “WebForm1” class then you could access it easily like “previousPage.SpookyValue”.



But to be able to do this all we needed to use “Server.Transfer”. With Response.Redirect all state information is lost. However with ASP.NET 2.0 we do have postbackURL property that comes in handy when we are talking about cross page PostBacks. Just by specifying “PostBackUrl” we can easily transfer the state of one page to the other page.



this.btnPostBack.PostBackUrl = "~/Default2.aspx";



On the receieving end of this postback, we can easily retrieve the page refrence that initiated this postback. This could be done using “PreviousPage” property. Following is code snippet that does the same:



if (this.PreviousPage != null && this.PreviousPage.IsCrossPagePostBack)

{

this.txtDetails.Text = "This is a cross Page Post Back and the source page is " + this.PreviousPage.ID;

this.txtDetails.Text += "Variables available at previous page: Comments = " + ((_Default)this.PreviousPage).Comments;

this.txtDetails.Text += "Value = " + ((_Default)this.PreviousPage).Value;

}





--------------------------------------------------------------------------------
Code for ASP.NET 1.X (WebForm2.aspx.cs)


--------------------------------------------------------------------------------



29: private void Page_Load(object sender, System.EventArgs e)
30: {
31: //this.Request.p
32: if(Context.Handler != null && Context.Handler is WebForm1)
33: {
34: previousPage = (WebForm1) Context.Handler;
35: }
36:
37: // Put user code to initialize the page here
38: ShowStateInformation();
39:
40: }
41:
64: /// Shows state information
65: ///
66: private void ShowStateInformation()
67: {
68: lblStateInformation.Text = "View State Informaiton goes like : " ;
69: IEnumerator viewStateEnum = this.ViewState.Keys.GetEnumerator();
70:
71: while(viewStateEnum.MoveNext() )
72: {
73: lblStateInformation.Text += "Viewstate Key = "+viewStateEnum.Current.ToString()+" and Value = "+this.ViewState[viewStateEnum.Current.ToString()].ToString()+" ";
74: }
75:
76: if(previousPage != null)
77: lblStateInformation.Text = "Spooky Info goes like "+previousPage.SpookyValue;
78: else
79: lblStateInformation.Text = "No state information available ";
80: }
81:
82: }
83: }

--------------------------------------------------------------------------------
Code for ASP.NET 1.X (WebForm1.aspx.cs)


--------------------------------------------------------------------------------


25: private string someSpookyVariable;
26:
27: public string SpookyValue
28: {
29: get
30: {
31: return this.someSpookyVariable;
32: }
33: }
34:
35:
36: private void Page_Load(object sender, System.EventArgs e)
37: {
38: if( this.ViewState["spookyStateKey"] != null)
39: {
40: Response.Write(" I have a spooky state variable "+this.ViewState["spookyStateKey"].ToString());
41: }
42: // Put user code to initialize the page here
43: ShowStateInformation();
44: }
45:
60: private void InitializeComponent()
61: {
62: this.btnRedirect.Click += new System.EventHandler(this.btnRedirect_Click);
63: this.Load += new System.EventHandler(this.Page_Load);
64:
65: htmlHiddenInfo = new HtmlInputHidden();
66: htmlHiddenInfo.ID="HiddenId";
67: htmlHiddenInfo.Name="HiddenName";
68: htmlHiddenInfo.Value="HiddenValue";
69: this.Controls.Add(htmlHiddenInfo);
70:
71: this.someSpookyVariable = "I am a spooky value that might be read from some other page!";
72:
73: }

79: private void ShowStateInformation()
80: {
81: lblStateInformation.Text = "View State Informaiton goes like : " ;
82: IEnumerator viewStateEnum = this.ViewState.Keys.GetEnumerator();
83: while(viewStateEnum.MoveNext() )
84: {
85: lblStateInformation.Text += "Viewstate Key = "+viewStateEnum.Current.ToString()+" and Value = "+this.ViewState[viewStateEnum.Current.ToString()].ToString()+" ";
86: }
87:
88: lblStateInformation.Text += "Hidden Var Info goes like : " ;
89: lblStateInformation.Text += "Name = "+htmlHiddenInfo.Name+" Id = "+htmlHiddenInfo.ID+" Value = "+htmlHiddenInfo.Value;
90: }
91:
92: private void btnRedirect_Click(object sender, System.EventArgs e)
93: {
94: if(this.ViewState["spookyStateKey"] == null)
95: {
96: this.ViewState.Add("spookyStateKey","spookyStateValue");
97: }
98:
99: Server.Transfer("WebForm2.aspx");
100:
101: }
102: }
103: }

--------------------------------------------------------------------------------

Code for ASP.NET 2.0 (_Default.aspx.cs)

--------------------------------------------------------------------------------


11: public partial class _Default : System.Web.UI.Page
12: {
13: ///
14: /// Comments to be exposed on the post back
15: ///
16: public string Comments
17: {
18: get
19: {
20: return this.txtComment.Text;
21: }
22: }
23:
24: ///
25: /// Value to be exposed on post back
26: ///
27: public string Value
28: {
29: get
30: {
31: return this.cmbValues.Text;
32: }
33: }
34:

39: protected void btnPostBack_Click(object sender, EventArgs e)
40: {
41: this.btnPostBack.PostBackUrl = "~/Default2.aspx";
42: }
43: protected void btnServerTransfer_Click(object sender, EventArgs e)
44: {
45: Server.Transfer("Default2.aspx");
46: }
47: protected void Button1_Click(object sender, EventArgs e)
48: {
49: Response.Redirect("Default2.aspx");
50: }
51: }
--------------------------------------------------------------------------------Code for ASP.NET 2.0 (Default2.aspx.cs)--------------------------------------------------------------------------------

1: protected void Page_Load(object sender, EventArgs e)
2: {
3: if (this.PreviousPage != null && this.PreviousPage.IsCrossPagePostBack)
4: {
5: this.txtDetails.Text = "This is a cross Page Post Back and the source page is " + this.PreviousPage.ID;
6: this.txtDetails.Text += "Variables available at previous page: Comments = " + ((_Default)this.PreviousPage).Comments;
7: this.txtDetails.Text += "Value = " + ((_Default)this.PreviousPage).Value;
8: }
9: else
10: {
11: if ( (this.Context.Handler != null) && (this.Context.Handler is _Default) )
12: {
13: this.txtDetails.Text = "This was a Server.Transfer Request";
14: this.txtDetails.Text += "Variables available at previous page: Comments = " + ((_Default)this.Context.Handler).Comments;
15: this.txtDetails.Text += "Value = " + ((_Default)this.Context.Handler).Value;
16: }
17: else
18: {
19: this.txtDetails.Text = "This was a Response.Redirect Request. No state available for calling page!";
20: }
21: }
22: }

--
Hammad Rajjoub,
Microsoft MVP (Most Valuable Professional),
Windows Server Systems: XML Web Services
Member Speakers Bureau,
Chairman UG Relations Committe,
INETA MEA.(http://mea.ineta.org)
Blog: http://dotnetwizards.blogspot.com

2 comments:

Muhammad Saqib Ilyas said...

Nice stuff.

Furqan said...

its nicer.;)