Search code examples
asp.netmaster-pagesnullreferenceexceptioncontent-pages

Unable to pass data from one content page to another using master page


I'm trying to make an asp webform that posts data to another webform.

I've made two separate projects, one that uses master page and one the doesn't.

Senario:

WebForm1.aspx has two text boxes and a submit button

<table>
        <tr>
            <td >Name:</td>
            <td >
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td class="auto-style1"></td>
        </tr>
        <tr>
            <td>Id:</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>

WebForm2.aspx.cs has two labels which should display the data received from WebForm1.aspx

Page prevPage = this.PreviousPage;
        if (prevPage != null)
        {
            Label1.Text = ((TextBox)prevPage.FindControl("TextBox1")).Text;
            Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
        }

Case 1: [Posting without master page]

The data is posted normally.

Case 2: [Posting with master page]

I get NullReferenceException.

So I broke down the code.

Page prevPage = this.PreviousPage;
        if (prevPage != null)
        {
            ControlCollection collec = prevPage.Controls;
            Control ctrl= prevPage.FindControl("TextBox1");
            TextBox txtbx = (TextBox)ctrl;
            Label1.Text = txtbx.Text; //Exception raised here

            Label2.Text = ((TextBox)prevPage.FindControl("TextBox2")).Text;
        }

While debugging: I executed "collec.Count" in the Immediate Window.

Case 1: [Posting without master page]

collec.Count returned 5

Case 2: [Posting with master page]

collec.Count returned 1 [ WHY? ]

Later,

I tried to pass data using public properties

WebForm1.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("WebForm2.aspx");
    }

    public string Name { get { return TextBox1.Text; } }
    public string ID { get { return TextBox2.Text; } }

WebForm2.aspx.cs

WebForm1 prevPage = (WebForm1)this.PreviousPage;
        if (prevPage != null)
        {
            ControlCollection c = prevPage.Controls;
            Label1.Text = prevPage.Name;
            Label2.Text = prevPage.ID;
        }

and now it works correctly, even with master page.

So could anybody explain me whats going on and why posting from one content page to another content page with master giving me NullReferenceException ?


Solution

  • First you must look in the Content Placeholder of the submitting page

    Therefore, the code would look more like this:

    ContentPlaceHolder placeHolder = (ContentPlaceHolder)PreviousPage.Master.FindControl("ContentPlaceHolder1");
            TextBox txt1 = (TextBox)placeHolder.FindControl("TextBox1");
    

    When you are using master page a TextBox with an id of TextBox1 inside a Content control tied to ContentPlaceHolder1 will have its id attribute extended like this:

    <input name="ctl00$ContentPlaceHolder1$TextBox1" type="text" id="ContentPlaceHolder1_TextBox1" />
    

    But when you are not using Master page there is no 'ContentPlaceHolder' so TextBox1 will rendered like this:

    <input name="TextBox1" type="text" id="TextBox1" />