我尝试将参数从一页传递到另一页时发生NullReferenceException [英] NullReferenceException while Im trying to pass parameters from one page to another

查看:70
本文介绍了我尝试将参数从一页传递到另一页时发生NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能帮助我解决问题,我将不胜感激.您好,我尝试将某些值从一页传递到另一页,但在运行时出现异常.

这是我的ASP代码:

I would be grateful if you could help me to solve the problem. Hello I try to pass some value from one page to another but at the run time i get exception.

Here is my ASP code:

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="PostBackUrlPage2cs.aspx" runat="Server"> 
  </asp:button>



以下是目标页面上的代码:



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.



我以msdn为例:
http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.button.postbackurl.aspx

为什么会出现此异常?如何解决此问题?

预先谢谢您.



I took the example from msdn:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx

Why do I get this exception?and how can I solve this problem?

Thank you in advance.

推荐答案

哦,我知道了.问题是FindControl返回null.使用此方法是一个常见的大错误.为什么?您可以通过显式声明的成员访问所有控件.

您可能会有拼写错误或其他错误,并且编译器无法检测到问题.

—SA
Oh, I see. The problem is that FindControl returns null. This is a big common mistake to use this method. Why? You have access to all your controls via the members which are explicitly declared.

You can have a misspelling or something, and you compiler cannot detect a problem.

—SA


尝试调试代码,查看错误发生在哪里.这将帮助您缩小问题的范围.
Try to debug your code and see where the error occurs. This will help you narrow down the problem.


这篇关于我尝试将参数从一页传递到另一页时发生NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆