You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

出现Object reference未实例化异常:ASP.NET页面传值求助

解决ASP.NET页面传值的NullReferenceException问题

看起来你把QueryString传值和**跨页提交(Cross-Page Posting)**两种方式搞混了,这才导致了NullReferenceException异常。咱们一步步理清楚问题,再给出两种正确的实现方案:

问题根源分析

  1. 你提到想用QueryString方法传值,但WebForm1的按钮用了PostBackUrl属性——这是跨页提交的用法,和QueryString的逻辑完全不匹配;
  2. 即使你想用跨页提交,代码里也有两个关键问题:
    • 按钮的PostBackUrl指向的是~/ResponseBack.aspx,但你要传值到WebForm2.aspx,这明显是笔误;
    • 直接用lastOne.FindControl("MAIL")找不到控件,因为你的TextBox嵌套在<table>容器里,Page.FindControl只能查找顶级控件,嵌套的控件需要递归查找;
    • 如果Context.Handler获取到的不是WebForm1的页面实例,也会返回null,触发异常。

方案1:使用QueryString传值(你最初想采用的方式)

这种方式是通过URL拼接参数传递数据,适合简单、非敏感的内容。

修改WebForm1的后台代码

在Button1_Click事件里拼接URL参数,替换原来的PostBackUrl方式:

protected void Button1_Click(object sender, EventArgs e)
{
    // 对参数进行URL编码,避免特殊字符导致的乱码或截断
    string encodedEmail = Server.UrlEncode(MAIL.Text);
    string encodedMessage = Server.UrlEncode(MESAGE.Text);
    
    // 跳转到WebForm2并拼接参数
    Response.Redirect($"WebForm2.aspx?Name=MAIN&Email={encodedEmail}&Message={encodedMessage}");
}

修改WebForm2的后台代码

直接从Request.QueryString中取值,不用再找前一个页面的控件:

protected void Page_Load(object sender, EventArgs e)
{
    string name = Request.QueryString["Name"];
    if (name == "MAIN")
    {
        if (!IsPostBack)
        {
            string fname = "Anonymous";
            string lname = "Anonymous";
            // 从QueryString获取值,用??处理null情况,避免空引用
            string email = Request.QueryString["Email"] ?? "";
            string mesg = Request.QueryString["Message"] ?? "";
            
            // 解码后赋值给Label
            LabelEMAIL.Text = Server.UrlDecode(email);
            LabelMESG.Text = Server.UrlDecode(mesg);
            LabelFNAME.Text = fname;
            LabelLNAME.Text = lname;
        }
        else
        {
            Response.Redirect("WebForm1.aspx");
        }
    }
}

方案2:使用跨页提交(Cross-Page Posting)

如果不想把参数暴露在URL里,可以用这种方式,通过Post请求传递数据。

修改WebForm1的按钮PostBackUrl

把按钮的目标页面改成WebForm2.aspx:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" PostBackUrl="~/WebForm2.aspx" Text="Submit" />

修改WebForm2的后台代码

添加一个递归查找控件的辅助方法,用来找到嵌套在容器里的TextBox:

protected void Page_Load(object sender, EventArgs e)
{
    // 判断是否是跨页提交过来的请求
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
        if (!IsPostBack)
        {
            string fname = "Anonymous";
            string lname = "Anonymous";
            
            // 递归查找嵌套的TextBox控件
            TextBox mailBox = FindControlRecursive(PreviousPage, "MAIL") as TextBox;
            TextBox msgBox = FindControlRecursive(PreviousPage, "MESAGE") as TextBox;
            
            // 先判断控件是否存在,再赋值,避免空引用
            if (mailBox != null)
            {
                LabelEMAIL.Text = mailBox.Text;
            }
            if (msgBox != null)
            {
                LabelMESG.Text = msgBox.Text;
            }
            
            LabelFNAME.Text = fname;
            LabelLNAME.Text = lname;
        }
        else
        {
            Response.Redirect("WebForm1.aspx");
        }
    }
}

// 递归查找控件的辅助方法
private Control FindControlRecursive(Control parentControl, string targetId)
{
    if (parentControl.ID == targetId)
    {
        return parentControl;
    }
    
    // 遍历所有子控件,递归查找
    foreach (Control childControl in parentControl.Controls)
    {
        Control foundControl = FindControlRecursive(childControl, targetId);
        if (foundControl != null)
        {
            return foundControl;
        }
    }
    
    return null;
}

内容的提问来源于stack exchange,提问作者Sobaan Ul Mujtaba Hashmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:02:32