LoadControl 和 Page_Load 事件未触发

发布于 2024-08-07 06:45:10 字数 620 浏览 7 评论 0原文

嘿大家!我通过 Web 方法加载 UserControl,并使用 LoadControl 功能:

// create page, stringWriter
Page _page = new Page();
StringWriter _writer = new StringWriter();

// get popup control
Controls_Popup_ForumThreadForm _control = _page.LoadControl("~/Controls/Popup_ForumThreadForm.ascx") as Controls_Popup_ForumThreadForm;

然后执行以下操作:

// add control to page
 _page.Controls.Add(_control);

HttpContext.Current.Server.Execute(_page, _writer, false);

问题是控件的 Page_Load 事件根本不触发。如果我添加另一个函数,并在将控件添加到页面的 Controls 集合之前调用它,则该函数将触发,但 Page_Load 事件不会触发。

有什么想法吗,伙计们?谢谢大家!

Hey all! I'm loading a UserControl through a Web Method, and using the LoadControl functionality as such:

// create page, stringWriter
Page _page = new Page();
StringWriter _writer = new StringWriter();

// get popup control
Controls_Popup_ForumThreadForm _control = _page.LoadControl("~/Controls/Popup_ForumThreadForm.ascx") as Controls_Popup_ForumThreadForm;

Then I do the following:

// add control to page
 _page.Controls.Add(_control);

HttpContext.Current.Server.Execute(_page, _writer, false);

Problem is that the Page_Load event of the control doesn't fire at all. If I add another function, and call it prior to adding the control to the Controls collection of the page, that function will fire, but the Page_Load event will not fire.

Any ideas, fellas? Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城挽邺 2024-08-14 06:45:10

据我所知,动态渲染控件时不可能执行事件。但我有一个技巧可以渲染有效的用户控件。

我使用一个函数来呈现一个用户控件,该控件采用用户控件的物理路径和属性列表。我在用户控件中定义了一个特殊属性,它可以像一个方法,当我设置它时运行我的自定义代码。这是渲染函数:

public static string RenderUserControl(string path, List<KeyValuePair<string, object>> properties)
    {
        Page pageHolder = new Page();
        UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
        viewControl.EnableViewState = false;
        Type viewControlType = viewControl.GetType();
        foreach (var pair in properties)
        {
            PropertyInfo property = viewControlType.GetProperty(pair.Key);
            if (property != null)
            {
                property.SetValue(viewControl, pair.Value, null);
            }
        }
        HtmlForm f = new HtmlForm();

        f.Controls.Add(viewControl);

        pageHolder.Controls.Add(f);
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);
        return (output.ToString());
    }

在需要渲染的用户控件中定义一个属性,例如 RunMyCode
设置您想要的任何属性,最后添加您定义的特殊属性。

data.Add(new KeyValuePair<string, object>("RunMyCode", SomeDataOrNothing));

在用户控件的呈现事件中,您可以检查 RunMyCode 属性的值,如果已设置,则手动调用事件通常会引发的方法。

Based on my knowledge it is not possible to execute events when dynamically rendering controls. But I have a trick in rendering user controls that works.

I use a function to render a user control that take physical path of user control, and a list of properties. I define a special property in the user control, that can be like a method and when I set it run my custom code. This is Render Function:

public static string RenderUserControl(string path, List<KeyValuePair<string, object>> properties)
    {
        Page pageHolder = new Page();
        UserControl viewControl = (UserControl)pageHolder.LoadControl(path);
        viewControl.EnableViewState = false;
        Type viewControlType = viewControl.GetType();
        foreach (var pair in properties)
        {
            PropertyInfo property = viewControlType.GetProperty(pair.Key);
            if (property != null)
            {
                property.SetValue(viewControl, pair.Value, null);
            }
        }
        HtmlForm f = new HtmlForm();

        f.Controls.Add(viewControl);

        pageHolder.Controls.Add(f);
        StringWriter output = new StringWriter();
        HttpContext.Current.Server.Execute(pageHolder, output, false);
        return (output.ToString());
    }

In the user control that you need to render define a property for example, RunMyCode
set any property that you want and at last add special property that you define.

data.Add(new KeyValuePair<string, object>("RunMyCode", SomeDataOrNothing));

In the render event of the user control, you can then check the value of the RunMyCode property and if it's set, manually call the methods that the events would normally raise.

天生の放荡 2024-08-14 06:45:10

检查 ASP.NET 页面生命周期概述

PreInit:启动阶段后引发已完成且在初始化阶段开始之前。

使用此事件进行以下操作:

Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.

Create or re-create dynamic controls.

Set a master page dynamically.

Set the Theme property dynamically.

Read or set profile property values.

check ASP.NET Page Life Cycle Overview

PreInit:Raised after the start stage is complete and before the initialization stage begins.

Use this event for the following:

Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.

Create or re-create dynamic controls.

Set a master page dynamically.

Set the Theme property dynamically.

Read or set profile property values.
々眼睛长脚气 2024-08-14 06:45:10

在页面生命周期的PreInit阶段添加控件:

var page = new Page();
var writer = new StringWriter();            
page.PreInit += new EventHandler((s, e) =>
{
    var control = page.LoadControl("");
    (Page)s).Controls.Add(control);
});
HttpContext.Current.Server.Execute(page, writer, false);

Add the control during the PreInit stage of the page life cycle:

var page = new Page();
var writer = new StringWriter();            
page.PreInit += new EventHandler((s, e) =>
{
    var control = page.LoadControl("");
    (Page)s).Controls.Add(control);
});
HttpContext.Current.Server.Execute(page, writer, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文