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

Techniques to send data from one webform to another in in asp.net - Part 58

Suggested Videos
Part 55 - Cross page postback
Part 56 - Cross page postback strongly typed reference
Part 57 - Opening new window using javascript in asp.net

Different techniques to move data from one webform to another 
1. Cross Page Postback: Discussed in Part 55 and Part 56
2. Context.Handler object - Will be discuss in this video session.

The following concepts will be discussed in the subsequent sessions
3. Query strings 
4. Cookies
5. Session state
6. Application state



In general, the members of one Web form are unavailable from a subsequently displayed Web form. However, when navigating between Web forms using the Transfer or Execute method, data can be retrieve from the previous Web form using Context.Handler object.

Points to remember Context.Handler object
1. Context.Handler returns WebForm1 as the previous page, only the first time when you land on WebForm2 from WebForm1. If there is a button on WebForm2, and if you click the button, the page will postback, and Context.Handler will return WebForm2 instead of WebForm1.
2. For the Context.Handler to return WebForm1 as the previous page, you should have landed on WebForm2, using Server.Transfer or Server.Execute method from WebForm1.
3. The control values from the previous page, can be accessed using FindControl() method or using public properties. The problem with FindControl() method is that, if you mis-spell the ControlID, we could get a runtime NullRefernceException. Using public properties, instead of FindControl() method, can eliminate runtime NullRefernceExceptions.



WebForm1.aspx HTML source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>
                This is WebForm1</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Button ID="Button1" runat="server" 
            Text="Go to WebForm2" onclick="Button1_Click" />
        </td>
    </tr>
</table>
</div>

WebForm1.aspx.cs Code:
protected void Button1_Click(object sender, EventArgs e)
{
    Server.Transfer("~/WebForm2.aspx");
}

public string Name
{
    get
    {
        return txtName.Text;
    }
}

public string Email
{
    get
    {
        return txtEmail.Text;
    }
}

WebForm2.aspx HTML source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>This is WebForm2</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm2.aspx.cs Code:
//On postback Context.Handler returns WebForm2
if (!IsPostBack)
{
    Page lastpage = (Page)Context.Handler;
    if (lastpage is WebForm1)
    {
        //Use FindControl() if public properties does not exist on the 
        //previous page(WebForm1). FindControl() may cause 
        //NullRefernceExceptions due to mis-spelled conrol Id's

        //lblName.Text = ((TextBox)lastpage.FindControl("txtName")).Text;
        //lblEmail.Text = ((TextBox)lastpage.FindControl("txtEmail")).Text;
                    
        //Using public properties can eliminate NullRefernceExceptions 
        lblName.Text = ((WebForm1)lastpage).Name;
        lblEmail.Text = ((WebForm1)lastpage).Email;
    }
}

3 comments:

  1. Sir kindly upload crystal reports or SSRS tutorials... thanks in advance

    ReplyDelete
  2. WebForm1 is not recognized in my page

    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.