Thursday, September 16, 2010

Passing Parameters in Pages and Asp.net State Management

I am going to do summary about the state management and pass parameters between pages in asp.net.

1. Use PreviousPage Property:

For example we have a button the property: PostBackUrl navigate to a sencond page, or a HyperLink or the first page use the Server.Transfer() method to open the sencond page. We want to pass the parameters in the first page to the 2nd page.

We can use the 2nd page's PreviousPage property. (2nd page has a label with name mylbl, and first page has a textbox name mytxt).

Code pretty much like:

mylbl.Text=(TextBox)PreviousPage.FindControl("mytxt").Text.

sometimes we place textbox in a panel(or some controls implemented INamingContainer), forexample our id for pane is mypanel.

We can use pretty much like:

Panel p=(Panel)PreviousPage.FindControl("mypanel");
TextBox tbox=p.FindControl("mytxt");

sometimes we can writh the previous page property in the page declaration, like:

< %@ PreviousPageType VirtualPath="~/.....aspx" % >

Notice one thing: Response.Redirect("sencondpage.aspx"), the PreviousPage property doesn't work here.

There are some client Side state management techniques can do parameters passing:

The advantage of the client side state management techniques is it stores the data in client side which will get server not that heavy burdon, the disadvantage is security.

Some techniques of statemanagement in client side are:

ViewState: but notice ViewState cannot pass parameters in the different pages, it can only pass the parameters in the same page.
ControlState and Hidden Field, they are like ViewState and cannot pass parameters across pages.

But for client side techniques, we can use cookie and querystring to pass parameters between different pages:

2. Cookie:

Code pretty much like:

Response.Cookie["txt"].Value=mytxt.Text.ToString(); //notice cookie value is string

We can also define expiration time for cookie,
Response.Cookie["txt"].Expires=System.DateTime.Now.AddDays(1);

in the sencond page:

mylbl.Text=Request.Cookie["txt"].Value;

But there are many bad things about cookie such as security issues.

3. Use QueryString.

In the first page:

Response.Redirect("~/Page2.aspx?para1="+mylbl.Txt.ToString()+"¶2=....",

In the 2nd page just use:

Request.QueryString["para1"] ...

there are also problems with QueryString such as length limitation et al.

There are also some state management object in server side, such as Session, Application, Cache. The good thing about the server side state management object is secure.

They can all be used to pass parameters between pages.

4. Session.

just the first page Session["txt"]=mytextbox.Text;
sencond page mylbl=Session["txt"].ToString();

Notice that session object is stored in server side, but session ID is stored in cookie. sometimes we use cookieless setting, we can set the session ID in querystring.

Session state can also be stored InProc(by default in server side), it can also be stored in StateServer(we can see there is an windows service called ASP.NET State Service, it runs the aspnet_state.exe), and it can be stored in SQLServer.

No comments: