Michael
Michael

Reputation: 13626

Getting NullReferenceException while im trying to pass parameters from one page to another

I try to pass some value from one page to another but at the run time I get exception.

Here is my ASP code:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

Enter a value to post:
<asp:textbox id="TextBox1" 
  runat="Server">
</asp:textbox>

<br /><br />

<asp:button id="Button1" 
  text="Post back to this page"
  runat="Server">
</asp:button>

<br /><br />

<asp:button id="Button2"
  text="Post value to another page" 
  postbackurl="Button.PostBackUrlPage2cs.aspx" 
  runat="Server">
</asp:button>
</asp:Content>

Here's the code-behind on the destination page:

 void Page_Load(object sender, System.EventArgs e)
    {
        string text;

        // Get the value of TextBox1 from the page that 
        // posted to this page.
        text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;

        // Check for an empty string.
        if (text != "")
            PostedLabel.Text = "The string posted from the previous page is "
                               + text + ".";
        else
            PostedLabel.Text = "An empty string was posted from the previous page.";
    }

I get this Exception:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I took the example from msdn.

Why do I get this exception?

Upvotes: 0

Views: 405

Answers (2)

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

well i am not sure but this is a typo error

postbackurl="Button.PostBackUrlPage2cs.aspx" incorrect

postbackurl="PostBackUrlPage2cs.aspx" is correct

<asp:button id="Button2"
  text="Post value to another page" 
  postbackurl="PostBackUrlPage2cs.aspx" 
  runat="Server">
</asp:button>

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Assuming you have this inside UpdatePanel, set the button as the post back trigger:

<Triggers>
    <asp:PostBackTrigger ControlID="Button2" />
</Triggers>

Upvotes: 1

Related Questions