
4.20.2002
Pass Data From One ASPX Page to Another Page
Damn - didn't realize that this had changed so much from regular ol' ASP. Setting the "action" attribute on a form element that points to another page doesn't work in ASP.NET. All server-side form controls (with runat="server") automatically post back to themselves -- when rendering the page, the runtime overwrites any value you have in the action attribute so the page will be posted back to itself.
Note: you can not simply remove the runat=server attribute from the form, as many people suggest. The runtime will throw an exception if there are any other server controls inside that form -- for the postback stuff to work, the form must be setup to submit back to itself.
MS suggests using Server.Transfer, but this has it's own complexities as to how to get the data from the first form to the second. However, looking at the help for Server.Transfer, there is an overload that offers a 2nd param that describes whether to clear form variables or not -- this suggests that the variables are immediately available to the page being transferred to???
- In the submitting code behind page...
- Declare public "get" properties in the submitting page that will expose the data from the controls you want.
- Call Server.Transfer( "pageToSubmitTo.aspx")
- In the receiving code behind page...
- Declare a public variable of the class for the submitting page, like this: public SubmittingPage submitter; (SubmittingPage is the class that is the code-behind for the submitting page)
- In the form_load method, call submitter = (SubmittingPage)Context.Handler;
- Now access the properties you exposed in the SubmittingPage class.
So basically, you pass control from the PostBack of Page1 to the Page_Load of Page2, and Page2 can access data in Page1. I guess when you call Server.Transfer, the context and data from Page1 is still alive and well....
Stuff I tried:
- Setting viewstate = false in submitting page doesn't seem to help with corrupt view state problem. In the link below, they elude to the fact that viewstate = false will allow you to send form variables through to next page. Doesn't work.
- Save Request.Form NameValueCollection to some public member in the submitting page. Then try to access that public member from the second page. Can't see it (even after casting the Context.Handler to the appropriate type). Could only see/use public properties on the submitting page (contrary to what article below says)
References:
MS KB - corrupt view state when call Server.Transfer
Comments:
Post a Comment