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
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
