【老生常谈的问题】未将对象引用设置到对象的实例

2019-01-02 20:33发布

第二个母版页报这个,未将对象引用设置到对象的实例
弹出登录成功后,报的错。

登录正常页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entity;
using System.Data;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] == null)
        {
            Response.Write("<script>alert('对不起,请登录!'),location.href='UserLogin.aspx'</script>");
        }
        else
        {
            Bind();
        }
    }
    public void Bind()
    {
        UserBusiness user = new UserBusiness();
        UserEntity ue = new UserEntity();
        ue.Uid = Session["uid"].ToString();
        DataTable dt = new DataTable();
        dt = user.FindUserInfo(ue);
        DataList1.DataSource = dt;
        DataList1.DataBind();

    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "more")
        {
            DataList1.SelectedIndex = e.Item.ItemIndex;
            Bind();
        }
        if (e.CommandName == "back")
        {
            DataList1.SelectedIndex = -1;
            Bind();
        }
        if (e.CommandName == "go")
        {
            Response.Redirect("UserBuy.aspx?pid=" + e.CommandArgument.ToString());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using Entity;
public partial class HomePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (tbx_name.Text != "" && tbx_password.Text != "" && tbx_RandNum.Text != "")
        {
            UserEntity ue = new UserEntity();
            ue.Uid= tbx_name.Text;
            ue.Upw = tbx_password.Text;
            UserBusiness ub = new UserBusiness();
            int result = ub.ValidateUnameAndPS(ue);
            if (result > 0)
            {
                Response.Write("<script>alert('登陆成功'),location.href='HomePage.aspx'</script>");
                Session["uid"] = tbx_name.Text.Trim();

            }
            else
            {
                Response.Write("<script>alert('用户名和密码不匹配')</script>");
            }

            if (tbx_RandNum.Text == Session["RandNum"].ToString())
            {

            }
            else
            {
                Response.Write("<script>alert('验证码输入错误')</script>");
            }

        }
        else
        {
            Response.Write("<script>alert('所有项必须写')</script>");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserRegiste.aspx");
    }
}

登录错误页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Entity;
using System.Data;
public partial class MasterPage2 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["aid"] == null)
        {
            Response.Write("<script>alert('对不起,请登录!'),location.href='AdministerLogin.aspx'</script>");

        }
        else
        {
            Bind();
        }
    }
    public void Bind()
    {
        AdministerInfoBusiness a2 = new AdministerInfoBusiness();
        AdministerInfoEntity a = new AdministerInfoEntity();
        a.Aid = Session["aid"].ToString();  
        DataTable dt = new DataTable();
        dt = a2.FindInfo(a);
        DataList1.DataSource = dt;
        DataList1.DataBind();
    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entity;
using Business;
public partial class AdministerLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "" && TextBox2.Text != "")
        {
            AdministerInfoEntity ad = new AdministerInfoEntity();
            ad.Aid = TextBox1.Text;
            ad.Apw = TextBox2.Text;
            AdministerInfoBusiness a = new AdministerInfoBusiness();
            int res = 0;
            res = a.ValidateAidAndPS(ad);
            if (res > 0)
            {
                Response.Write("<script>alert('登陆成功'),location.href='AdministerMain.aspx'</script>");
                Session["aid"] = TextBox1.Text.Trim();
            }
            else
            {
                Response.Write("<script>alert('用户名和密码不匹配')</script>");
            }
            if (TextBox3.Text == Session["RandNum"].ToString())
            {

            }
            else
            {
                Response.Write("<script>alert('验证码输入错误')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('所有项必须写')</script>");
        }
    }
}

应该是session的问题 但是第一个登录是正常显示的,在网上看到的方法,定位到session为空,断点调了下发现 控件的值为空。
看大佬们的解决办法 最开始是怀疑 代码顺序问题 先跳转了session未传出去,但是正常登录的那个是可以的啊。后来看的一些解决办法也有一些,但还是找不到根上。考虑用cookie了重新弄了,但是这个问题是什么啊,强迫症啊,想解决。难受。

标签:
2条回答
裙下三千臣
2楼-- · 2019-01-02 20:50

session值已经传出去了,但是接收值的控件有问题。我把那个控件删除,后正常了。

查看更多
柔情千种
3楼-- · 2019-01-02 20:59

毫不客气的说 。这种代码 在外面企业开发 90%看不到

查看更多
登录 后发表回答