asp:用户控件中的文字控件 null

asp:Literal control null in user control(asp:用户控件中的文字控件 null)
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)