gpt4 book ai didi

c# - 分配 Session 变量/值时出现 NullReferenceException

转载 作者:行者123 更新时间:2023-11-30 12:51:48 25 4
gpt4 key购买 nike

我在我的 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 不为空,值也不为空。

对此有什么想法吗?

编辑:

为属性所在的 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;
}
}
}
}

最佳答案

我几乎完全确定 NullReferenceException 是在 SetComment() 中抛出的,因为 CoreComment 的子控件(lblComment, lblCommentInfo) 在您设置 _Comment 属性时正确实例化。

这些子控件未实例化的原因确实是您当前添加 CoreComment 控件的方式。要动态添加 UserControl,您必须使用 Page.LoadControl()(请参阅:here)创建控件的新实例,因为它会执行一些幕后魔术以确保它是正确初始化,包括子控件的实例化。

旁注,我个人会将 SetComment() 更改为 SetComment(CoreCommons.System.Comment comment) 并使用参数而不是重复调用 getter,或者,如果继续使用原始方法,至少只调用一次 getter 并将结果存储在局部变量中。我假设可能是 InProc session 存储,这不会有太大区别,但在任何其他存储模式下,您会无缘无故地重复反序列化 Comment 对象。

关于c# - 分配 Session 变量/值时出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6005077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com