【发布时间】:2021-01-06 16:50:52
【问题描述】:
我在 ASP.NET 项目中的许多地方都使用了 Session 变量来存储数据。我通常会这样写:
public uint MyPropery
{
get
{
object o = Session["MyProperty"];
if (o != null)
return (uint)o;
else
return 0;
}
set
{
Session["MyProperty"] = value;
}
}
但是,这次我在 setter 中得到了 NullReferenceException。据我所知,以上述方式分配 Session 变量是有效的。另外,Session 不为空,value 也不为空。
对此有什么想法吗?
编辑:
为该属性所在的 UserControl 添加代码。我正在使用 ext.net,但这应该与此无关。我脑海中闪过一个念头:
UserControl(见下文)在页面的代码隐藏中动态添加。这和它有关系吗?
我正在添加这样的用户控件(在页面上):
foreach(CoreCommons.System.Comment c in cg.Reply_Comments)
{
WebApplicationExtNetTest.Secure.UserControls.CoreComment cc = new UserControls.CoreComment();
cc._Comment = c; // here is where i get the NullRef
this.Panel1.ContentControls.Add(cc);
}
标记:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CoreComment.ascx.cs" Inherits="WebApplicationExtNetTest.Secure.UserControls.CoreComment" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<ext:Panel runat="server" ID="CoreCommentOuterPanel" BodyStyle="background: #FFFDDE">
<Items>
<ext:ColumnLayout runat="server">
<Columns>
<ext:LayoutColumn ColumnWidth="0.8">
<ext:Image runat="server" ImageUrl="/Resources/bullet_triangle_green_16x16.png" Align="AbsMiddle"></ext:Image>
<ext:Label runat="server" ID="lblCommentInfo"></ext:Label>
</ext:LayoutColumn>
<ext:LayoutColumn ColumnWidth="0.2"><ext:Button runat="server" ID="btnDelete" Icon="Delete"></ext:Button></ext:LayoutColumn>
</Columns>
</ext:ColumnLayout>
<ext:Label runat="server" ID="lblComment"></ext:Label>
</Items>
</ext:Panel>
代码隐藏:
namespace WebApplicationExtNetTest.Secure.UserControls
{
public partial class CoreComment : System.Web.UI.UserControl
{
public CoreCommons.System.Comment _Comment
{
get
{
object o = Session["CoreComment_ObjectId"];
if (o != null)
return (tWorks.Core.CoreCommons.System.Comment)o;
else
return null;
}
set
{
Session["CoreComment_ObjectId"] = value;
SetComment();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void SetComment()
{
if (_Comment == null)
{
lblCommentInfo.Text = "";
lblComment.Text = "";
}
else
{
lblCommentInfo.Text = _Comment.Author + ", " + _Comment.TimeStamp.ToString("g");
lblComment.Text = _Comment.Text;
}
}
}
}
【问题讨论】:
-
您发布的代码看起来应该可以工作。您能否发布失败的实际示例?
-
我一直使用相同的模式没有问题。我说问题出在其他地方。您可以在会话不存在时设置值
-
@SirViver:它有很多代码,是项目的一部分。它在一个 UserControl 中,我已经更新了帖子并发布了整个 UserControl。
-
@Jeff:会话必须存在,因为我已登录该站点。嗯……
-
正如我在更新后的帖子中所写:问题是否与我动态添加这些控件的事实有关?
标签: c# .net asp.net session nullreferenceexception