当前位置: 代码迷 >> ASP.NET >> 页面间传值之未将对象引用设立到对象的实例
  详细解决方案

页面间传值之未将对象引用设立到对象的实例

热度:5896   发布时间:2013-02-25 00:00:00.0
页面间传值之未将对象引用设置到对象的实例
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class ResultsPage : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        try        {           [color=#FF0000] string username = ((TextBox)PreviousPage.FindControl("usename")).Text;[/color]            string passward = ((TextBox)PreviousPage.FindControl("passward")).Text;          //  labelResult.Text = String.Format("{0}  {1}", usename, passward);        }        catch(Exception a)        {            labelResult.Text = a.ToString();        }        /*catch        {            labelResult.Text = "It's Wrong!";        }*/    }}

调试至红色的语句,捕获错误"System.NullReferenceException: 未将对象引用设置到对象的实例。 在 ResultsPage.Page_Load(Object sender, EventArgs e) 位置 c:\Users\yuanhuihua\Documents\Visual Studio 2010\WebSites\EventRegistrationWeb\ResultsPage.aspx.cs:行号 15".
求大侠指教!在线等。

------解决方案--------------------------------------------------------
(TextBox)PreviousPage.FindControl("usename")这控件都不存在啊,你是怎样跳转的
------解决方案--------------------------------------------------------
C# code
 TextBox tb=(TextBox)PreviousPage.FindControl("usename"); string username = tb.Text;
------解决方案--------------------------------------------------------
PreviousPage为Page类的一个公共属性。如果源页面和目标面位于同一个网站应用程序中,则目标页中的PreviousPage属性会包含对源页的引用,如果不是,则不会初始化PreviousPage属性。
------解决方案--------------------------------------------------------
原页面是POST到目标页面的,这样PreviousPage属性才会初始化。你若是要原页面里面什么也不写,也可。在原页面里放一个Button控件,这个Button的PostBackurl的值为目标页面。
------解决方案--------------------------------------------------------
你的对象不要嵌套在别的控制里面,如GridView,直接放在<form runat=server>里面
------解决方案--------------------------------------------------------
((TextBox)PreviousPage.FindControl("passward")).Text;这种获取另一个页面控件值应该是使用
Server.Transfer()跳转页面的

在另一个页面后台跳转要这样写,Server.Transfer("ResultsPage.aspx");
------解决方案--------------------------------------------------------
测试通过
源页面 HTML部分:
HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="THJS.JxBuilder.Web.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>无标题页</title></head><body>    <form id="form1" runat="server">    <div>    <asp:TextBox ID="test" runat="server" ></asp:TextBox>    <asp:Button runat="server" ID="testButton" Text="提交" PostBackUrl="~/WebForm2.aspx" />    </div>    </form></body></html>
------解决方案--------------------------------------------------------
string username = ((TextBox)PreviousPage.FindControl("usename")).Text;

这段代码抛出未将对象引用设置到对象实例。造成这种情况的有两种原因,1.PreviousPage=null;2.Previous页内没有usename这个控件。解决办法就是确保能获取这两个值就OK啦。

我看你在24楼说的源页面是母版页,那怎么能用PreviousPage获取呢?母版页可以用this.Master啊,获取里边的控件与前一个页面类似。
------解决方案--------------------------------------------------------
你看看源文件有木有这个控件,然后看看这个控件有木有变化。。
------解决方案--------------------------------------------------------
  相关解决方案