Label1.Text怎么会提示未将对象引用设置到对象的实例
网页中做了个用户登录区域,前台部分代码:
<div id="welcom" runat="server" class="bluehyperlink">
欢迎访问:<asp:Label ID="Label1" runat="server" NavigateUrl="#" Text="Label"></asp:Label>
/
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="modipass.aspx" Text="修改口令"></asp:HyperLink>
</div>
发布后进入网页,输入用户名和口令,登录后无法打开新网页,提示“System.NullReferenceException: 未将对象引用设置到对象的实例”,并指出是Label1.Text = Request.Cookies["yhxm"].Value.ToString()这句出错,但在本机VS2010调试中没有问题。
新网页 Page_Load事件代码
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["yhxm"];
if (cookie == null)
{
denglu.Visible = true;
welcom.Visible = false;
HyperLink1.NavigateUrl = "login.aspx?url=" + Request.RawUrl;
haihua.Visible = false;
kehu.InnerHtml = "<ul><li><a href=\"system.aspx\" Target=\"_blank\">系统首页</a></li><li><a>入库申请</a></li><li><a>出库申请</a></li><li><a>入库查询</a></li><li><a>出库查询</a></li></ul>";
}
else if (Request.Cookies["yhxm"].Value != "" || Request.Cookies["yhxm"].Value != null)
{
denglu.Visible = false;
welcom.Visible = true;
Label1.Text = Request.Cookies["yhxm"].Value.ToString();/////////////////////////////提示这句出错
if (Request.Cookies["ishaihua"].Value == "0")
{
haihua.Visible = false;
kehu.Visible = true;
}
else
{
haihua.Visible = true;
kehu.Visible = false;
}
}
}
开始以为是 Request.Cookies["yhxm"].Value出错,但测试后Request.Cookies["yhxm"]能读出数据。
后将出错这句改成Label1.Text = “能够读取数据”来测试,居然还是报错。我就不懂了Label1.Text =怎么还出错了,VS里能通过的。