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
        {
            string username = ((TextBox)PreviousPage.FindControl("usename")).Text;
            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".
求大侠指教!在线等。

解决方案 »

  1.   

    (TextBox)PreviousPage.FindControl("usename")这控件都不存在啊,你是怎样跳转的
      

  2.   


     TextBox tb=(TextBox)PreviousPage.FindControl("usename");
     string username = tb.Text;页面传值的方法很多啊,参考http://www.cnblogs.com/liukemng/archive/2010/12/04/1895966.html
      

  3.   

    控件在主页中default.aspx的页面定义了
      

  4.   

    lz,b/s的程序不像是你那样做的
      

  5.   

    PreviousPage为Page类的一个公共属性。如果源页面和目标面位于同一个网站应用程序中,则目标页中的PreviousPage属性会包含对源页的引用,如果不是,则不会初始化PreviousPage属性。
      

  6.   

    楼主若是硬要指定页面A.aspx是页面B.aspx页面的源页面,可在B.aspx页面头部加上强引用。
    <@ PreviousPageType VirtualPath="~A.aspx" %> 
      

  7.   

    default.aspx.csusing System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
    }原页面应该没有什么代码的。
      

  8.   

    原页面是POST到目标页面的,这样PreviousPage属性才会初始化。你若是要原页面里面什么也不写,也可。在原页面里放一个Button控件,这个Button的PostBackurl的值为目标页面。
      

  9.   

    在button控件的属性上我对PostBackurl的值已设置为目标页面了,但是他还是提示未将对象引用设置到对象的实例。
      

  10.   

    你的对象不要嵌套在别的控制里面,如GridView,直接放在<form runat=server>里面
      

  11.   

    额,还是有点不太懂,能否说得再详细点?是说在.aspx的文件中的源里面的代码么?
      

  12.   

    a.aspx<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="a.aspx.cs" Inherits="a" %><!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:Label ID="label" runat="server" Text="Label"></asp:Label>
            
        </div>
        <a href="source.aspx">source.aspx</a>
        </form>
    </body>
    </html>source.aspx<%@ Page Language="C#" AutoEventWireup="true" CodeFile="source.aspx.cs" Inherits="source" %><!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:DropDownList ID="dropDownList" runat="server">
                <asp:ListItem>A is a</asp:ListItem>
                <asp:ListItem>B is b</asp:ListItem>
                <asp:ListItem>C is c</asp:ListItem>
            </asp:DropDownList>
            <br />
            <asp:TextBox ID="username" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="SubmitButton" runat="server" PostBackUrl="~/a.aspx" 
                Text="Submit" />
        
        </div>
        </form>
    </body>
    </html>
    在线坐等大侠指点啊!
      

  13.   

    ((TextBox)PreviousPage.FindControl("passward")).Text;这种获取另一个页面控件值应该是使用
    Server.Transfer()跳转页面的在另一个页面后台跳转要这样写,Server.Transfer("ResultsPage.aspx");
      

  14.   

    把string passward = ((TextBox)PreviousPage.FindControl("passward")).Text;替换成
    string passward = Request.Form["username"]看看
      

  15.   

    source.aspx.csusing System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;public partial class source : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Server.Transfer("a.aspx");
        }
    }您说是加在那个地方么,运行之后,那个错误依然没有解决。
      

  16.   

    测试通过
    源页面 HTML部分:<%@ 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>源页面 cs部分:没写任何代码
    目标页 HTML部分:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="THJS.JxBuilder.Web.WebForm2" %><!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:Label ID="testLable" runat="server"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    目标页 cs部分:using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;namespace THJS.JxBuilder.Web
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) 
                {
                    testLable.Text = ((TextBox)Page.PreviousPage.FindControl("test")).Text;
                }
            }
        }
    }
      

  17.   

    谢谢了,我试了你的代码,发现你的确实能够得到正确的结果。但是换上我自己的代码,里面的源页面是母版,而我的各个DropDownList,TextBox等控件都放在一个2X5的表格中,然后按你这样来,却还是出现那个问题,请问这个和母版作源页面和控件在表格有没关系?为了这个问题,真的要疯了!!!
      

  18.   

    string username = ((TextBox)PreviousPage.FindControl("usename")).Text;这段代码抛出未将对象引用设置到对象实例。造成这种情况的有两种原因,1.PreviousPage=null;2.Previous页内没有usename这个控件。解决办法就是确保能获取这两个值就OK啦。我看你在24楼说的源页面是母版页,那怎么能用PreviousPage获取呢?母版页可以用this.Master啊,获取里边的控件与前一个页面类似。
      

  19.   

    我试了,用this.Master从母版页取值,问题依然没有解决。
      

  20.   

    不就是传个username么
    非要搞什么FindControl
    直接把你的连接这么写不就完了?Server.Transfer("a.aspx?username ="+你要穿的值,比如username);
    然后另一个界面获取
    string username = Resqurest.QueryString["username"].ToString();
    这样就能获取了
      

  21.   

    我看得 蛋疼 LZ你要另一个页面 获取这个页面的控件值 跳转的时候用URL方式 传过去不就OK了
      

  22.   

    额,我解决了,可能当时对这个问题有更深入的认识,现在发现,原来只是一个如何获取使用母版页的页面中的控件的问题。至于为什么非要用这种方法,只是在学习C#入门经典时按书上所做得不到验证才如此一定用这种方法。我把这个链接给出如下,希望能帮到后来人。
    http://blog.csdn.net/learnren/article/details/4075508
    最后谢谢大家了。