读书人

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

发布时间: 2012-08-09 15:59:22 作者: rapoo

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

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啊,获取里边的控件与前一个页面类似。
[解决办法]
你看看源文件有木有这个控件,然后看看这个控件有木有变化。。
------解决方案--------------------


不就是传个username么
非要搞什么FindControl
直接把你的连接这么写不就完了?Server.Transfer("a.aspx?username ="+你要穿的值,比如username);
然后另一个界面获取
string username = Resqurest.QueryString["username"].ToString();
这样就能获取了
[解决办法]
我看得 蛋疼 LZ你要另一个页面 获取这个页面的控件值 跳转的时候用URL方式 传过去不就OK了

读书人网 >asp.net

热点推荐