2012-01-10 81 views
0

我尝试从一个页面传递一些值到另一个页面,但在运行时我得到异常。即时通讯尝试从一个页面传递参数到另一个页面时获取NullReferenceException

这里是我的ASP代码:

<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> 

这里的目标页面的代码隐藏:

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."; 
    } 

我得到这个异常:

异常详细信息:系统。 NullReferenceException:未将对象引用设置为对象的实例。

我以msdn为例。

为什么我会得到这个异常?

+0

在哪一行你会得到NullReferenceException? – 2012-01-10 13:09:59

回答

1

假设你有这里面的UpdatePanel,设置按钮后回到触发:

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

好吧,我不知道,但,这是一个错字错误

一项PostBackUrl =“Button.PostBackUrlPage2cs.aspx “不正确

一项PostBackUrl =” PostBackUrlPage2cs.aspx”是正确的

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