asp:Literal control null in user control(asp:用户控件中的文字控件 null)
问题描述
我有一个包含 asp:Literal 的用户控件.
<asp:Literal id="MenuContainer" runat="server"/></div>代码隐藏页面中有一个初始化控件的方法:
internal void Setup(MyBusinessObject obj){MenuObject menu = MenuHelper.GetMenu(obj.State);如果(obj == null)MenuContainer.Visible = false;//其他代码}
在使用控件的页面中,我在 LoadComplete 事件处理程序中调用 Setup 控制方法(我首先在 Load 中调用它事件).无论 MyBusinessObject 是 null 还是非 null,当我在用户控件上访问 Literal 时,我都会收到错误:
对象引用未设置为对象的实例.
这是什么原因,有什么补救措施?
解决方案 很简单.正如 Rick Sthral 在他的一篇文章中所建议的那样,我在 web.config 的控制部分添加了一些东西( :( 了解帖子,你必须在他的页面上搜索).
很好地允许我在不放置 @Register 标记的情况下添加控件,但缺点是我的控件上的子控件显示为空!所以我只是将 @Register 指令放在我的页面中,它就起作用了.
I've a user control which contains asp:Literal.
<div>
<asp:Literal id="MenuContainer" runat="server" />
</div>
There is a method in code-behind page which initializes the control:
internal void Setup(MyBusinessObject obj)
{
MenuObject menu = MenuHelper.GetMenu(obj.State);
if(obj == null)
MenuContainer.Visible = false;
//other code
}
In the page where the control is used I call Setup method of control in LoadComplete event handler (I was first calling it in Load event). Irrespective of MyBusinessObject being null or not null, when I access Literal on user-control I get error:
Object reference not set to an instance of an object.
What is the reason and what is the remedy for this?
解决方案 It was very simple. I was adding things in web.config's controls section as was suggested by Rick Sthral in one of his post ( :( for got about the post, you will have to search on his page).
It was nicely allowing me to add controls without putting @ Register tag but the downside was that child controls on my controls were shown as null! so I simply put @ Register directive in my pages and it worked.
这篇关于asp:用户控件中的文字控件 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asp:用户控件中的文字控件 null


基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01