Thursday, June 19, 2008

Passing viewstates of a page to another page

Download the source code

Sometimes we need to pass information of one page to the next page to do this there are different approaches one of them is that you can pass just a querystring then lookup data in your data source. However sometimes there are no data sources so you need to have all states of one page in another page. So you may use Session object to pass this information. However, this may not be the best way because you are using Session object which is shared between different pages for a user. There is another way which is PostBackUrl.(In next posts I will compare State Management in asp.net)

How to pass viewstate by PostBackURL

After Visualstudio 2005 all our buttons has a nice properties called PostBackUrl which can be assigned to a page then state of this page (the page that has button with PostBackUrl) will be passed to Next page.

Take a look at this:

In this example we have a textbox and a dropdownlist and we need to see state of these two controls in next page. Also we have a button that its PostBackUrl is set to a page. When user click on this button the state is passed to next page to access these states you need to use PreviousPage object. Remember if someone tries to access this page by giving this page address into browser or user is redirected from other redirection method Then PreviousPage object is null. So to be in safe side always check this object. Take a look to this code:

if (PreviousPage != null)

{

DropDownList lst = PreviousPage.FindControl("DropDownList1") as DropDownList;

if (lst != null)

LabelDropdown.Text = lst.Text;

TextBox txt = PreviousPage.FindControl("TextBox1") as TextBox;

if (txt != null)

LabelTextbox.Text = txt.Text;

}

As you see in the code we are checking PreviousPage object so we are sure that previous page state is passed to this page it means that redirection method was PostBackUrl.

Also we have another method to make controls type safe which is defining PreviousPageType Directive (in html code) but the issue with this approach is that you can define one page as your previous page. Also if user access to this page with any other of redirection method then an error will be generated. So I highly recommend you use the previous approach and never user PreviousPageType directive.

Let's see the steps:

1- Define a page with a button which its PostBackUrl is filled with a page ( we call it target page)

2- In Target page Use PreviousPage.FindControl to access controls in previous page.

3- Always check PreviousPage object to be sure it is not null

Download the source code

6 comments:

hamid_h671 said...

hi dear teacher
Can I run my .NET Application without the .NET Framework?
please help me .
tanks
hamid (sazehgostar)

Nahid said...

GOOD JOB EMAD :x

Emad Yazdanpanah said...

Hi Hamid,
you can not run .net applications without .net framework.you may create a setup application that install .net framework.

shahriar said...

Also We Can Use .net 3.5 SP1 that can enabale Client Profile Setup Package. Please Read This post:
http://weblogs.asp.net/scottgu/archive/2008/05/12/visual-studio-2008-and-net-framework-3-5-service-pack-1-beta.aspx

although we can't abstinence .net framework.

Anonymous said...

Please Read this post:

http://weblogs.asp.net/scottgu/archive/2008/05/12/visual-studio-2008-and-net-framework-3-5-service-pack-1-beta.aspx

Paragraph about .NET Framework Client Profile Setup Package.

kourosh said...

Dear Emad,
First of all thanks for the great job you are doing for us.In addition I appreciate if you let me know where I can get some helpful Tips for Unit and integration Tests?
regards,
Kourosh.