Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Cross page postback strongly typed reference - Part 56

Suggested Videos
Part 53 - Server.Transfer in asp.net
Part 54 - Server.execute in asp.net
Part 55 - Cross page postback

The following are the different page navigation techniques in asp.net
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect - Discussed in Part 52
3. Server.Transfer - Discussed in Part 53
4. Server.Execute - Discussed in Part 54
5. Cross-Page postback - Discussed in Part 55
6. Window.Open 



In Part 55, we have discussed the basics of cross page posting. In the previous session, we used FindControl() method to get a reference to the TextBox on the previous page. The problem with FindControl() method is that, if we mis-spell the ID of the control, we don't get any compile time errors, but may cause runtime nullreference exceptions.

In this session, we will discuss about, obtaining a strongly types reference to the previous page. There are 2 ways to obtain a strongly typed reference. We will explore both of these options in this part of the video. We will be using the same example used in Part 55.

The first step in obtaining a strongly typed reference, is to create public properties. We want to 
convert the values of TextBox controls(txtName and txtEmail) into properties(Name and Email) respectively. The Name and Email properties are created as Read-Only properties, as we just need to read the values on the destination page.
//Name - read only property
public string Name
{
    get
    {
        return txtName.Text;
    }
}
//Email - read only property
public string Email
{
    get
    {
        return txtEmail.Text;
    }
}



The next step is to obtain a strongly typed reference to the previous page and access the public propertie as shown below. This code must be in the Page_Load event on WebForm2.aspx.cs. If Name or Email properties are mis-spelled, we get an immediate compile time error. Hence, strongly typed references can eliminate runtime nullreference exceptions.
protected void Page_Load(object sender, EventArgs e)
{
    //Type cast PreviousPage to WebForm1
    WebForm1 previousPage = (WebForm1)Page.PreviousPage;
    if (previousPage != null && previousPage.IsCrossPagePostBack)
    {
        //Access the Name and Email public properties
        lblName.Text = previousPage.Name;
        lblEmail.Text = previousPage.Email;
    }
    else
    {
        lblStatus.Text = "You landed on this page using a technique other than cross page post back";
    }
}

PreviousPageType directive can also be used to obtain, a strongly typed reference to the previous page. In our example, for WebForm2, the previous page is WebForm1. So, in the HTML source of WebFOrm2.aspx paste the line below, after the Page directive.
<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>

In the code behind file, this.PreviousPage property or PreviousPage(without any prefix), returns a strongly typed reference to WebForm1. Please note, that Page.PreviousPage property, still returns the loosely typed Page object.
protected void Page_Load(object sender, EventArgs e)
{
    //this.PreviousPage returns a stronly typed reference
    //WebForm1 previousPage = this.PreviousPage;
    //PreviousPage also returns a stronly typed reference
    WebForm1 previousPage = PreviousPage;
    //Page.PreviousPage returns loosely typed reference
    //Page previousPage = Page.PreviousPage;
    if (previousPage != null && previousPage.IsCrossPagePostBack)
    {
        //Access the Name and Email public properties
        lblName.Text = previousPage.Name;
        lblEmail.Text = previousPage.Email;
    }
    else
    {
        lblStatus.Text = "You landed on this page using a technique other than cross page post back";
    }
}

So in short to obtain a strongly typed reference, there are 2 very simple steps.
First Step – Create Public Properties (Read-Only is sufficient)
Second Step – Obtain a strongly typed reference by TypeCasting or by using the PreviousPageType directive.

5 comments:

  1. sir i m unable to write webform1 in webform2 while type casting.the intellisense does not show the webform1 when typed.how to resolve it.

    ReplyDelete
  2. In place of "WebForm1" you should write ur page name from where u r redirecting

    ReplyDelete
  3. please cover @PreviousPageType TypeName property too

    ReplyDelete
  4. I have the same issue as Rahul.
    My webforms are also calles WebForm1 and WebForm2. In webform1 i can only see webform1 and in webform2 i can only webform2.

    Did anyone know how to solve this. on the yputube page i also didn't find the solution.

    am i missing piece of code? wich is not mentioned.

    ReplyDelete
  5. Dear Rahul sharma & Kishen Biekhram,
    Write class name with proper namespace.
    E.g. namespace.WebForm1

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.