加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Asp.net UserControl LoadControl问题

发布时间:2020-12-15 23:37:31 所属栏目:asp.Net 来源:网络整理
导读:使用LoadControl(type,Params)时,我遇到了一个问题.让我解释… 我有一个超简单的用户控制(ascx) %@ Control Language="C#" AutoEventWireup="True" Inherits="ErrorDisplay" Codebehind="ErrorDisplay.ascx.cs" EnableViewState="false" %asp:Label runat="s
使用LoadControl(type,Params)时,我遇到了一个问题.让我解释…

我有一个超简单的用户控制(ascx)

<%@ Control Language="C#" AutoEventWireup="True" Inherits="ErrorDisplay" Codebehind="ErrorDisplay.ascx.cs" EnableViewState="false" %>

<asp:Label runat="server" ID="lblTitle" />
<asp:Label runat="server" ID="lblDescription" />

与代码(c#)后面:

public partial class ErrorDisplay : System.Web.UI.UserControl
{

    private Message _ErrorMessage;    

    public ErrorDisplay()
    {
    }

    public ErrorDisplay(Message ErrorMessage)
    {
        _ErrorMessage = ErrorMessage;
    }    

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (_ErrorMessage != null)
        {
            lblTitle.Text = _ErrorMessage.Message;
            lblDescription.Text = _ErrorMessage.Description;
        }
    }
}

在我的Web应用程序的其他地方,我将使用以下代码向用户控件添加一个实例:

divValidationIssues.Controls.Add(LoadControl(typeof(ErrorDisplay),new object[] { MessageDetails }));

我正在使用LoadControl的重载版本,因为我想将Message参数传递给构造函数.所有这一切似乎都可以正常工作.

但是,当在ErrorDisplay用户控件上触发OnPreRender()时,lblTitle和lblDescription变量都为空,尽管它们具有等价的标记.消息变量已正确填充.

任何人都可以说明为什么会发生这种情况吗?

谢谢

编辑:

为了清楚起见,我还会补充说,正在编程地将用户控件添加到页面的代码正在响应按钮按钮,所以“主机页面”已经通过Init,Page_Load进行,现在正在处理事件处理程序.

我们无法在早期的asp生命周期阶段添加用户控件,因为它们是响应于按钮单击事件而创建的.

解决方法

我也尝试了以下代码 – 它产生相同的结果(即lblTitle和lblDescription都为null)
protected void Page_Load(object sender,EventArgs e)
{
    if (_ErrorMessage != null)
    {
        lblTitle.Text = _ErrorMessage.Message;
        lblDescription.Text = _ErrorMessage.Description;
    }
}

我了解到,LoadControl函数将控件加载到正在包含的页面的当前“状态”中.因此,Init,Page_Load等都作为LoadControl调用的一部分运行.

有趣的是,这个(未回答)asp.net论坛的帖子展现了与我遇到的相同的问题.

MSDN Forums Post

另外 – 从MSDN:

When you load a control into a container control,the container raises all of the added control’s events until it has caught up to the current event. However,the added control does not catch up with postback data processing. For an added control to participate in postback data processing,including validation,the control must be added in the Init event rather than in the Load event.

所以不要LoadControl正确的控制?

编辑:

好的,所以我在这里回答我自己的问题

我找到一个回复??版本的论坛帖子,我链接到Here上面

基本上,答案是LoadControl(type,params)不能推断“页面前端”ascx来解析,因此它不会打扰任何控件.当您使用LoadControl(“ascx路径”)版本时,它被赋予页面前端,因此执行所有解析和初始化.

总而言之,我需要更改正在控制的代码,并将其拆分成单独的部分.即

Control ErrorCntrl = LoadControl("ErrorDisplay.ascx");
ErrorCntrl.ID = SomeID;
(ErrorCntrl as ErrorDisplay).SetErrorMessage = MessageDetail;
divErrorContainer.Controls.Add(ErrorCntrl);

它应该工作正常..它不像我以前的尝试一样整洁,但至少它会工作.

我仍然乐意提出上述建议.

干杯

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读