使用 C# 代码加载 ascx 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/841161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:44:30  来源:igfitidea点击:

Load ascx component using C# code

c#asp.netascx

提问by AlexH

Is there any way that I can use C# to load and then "render" an ascx control?

有什么方法可以使用 C# 加载然后“渲染”一个 ascx 控件?

Essentially I am trying to replace inline ASP with a C# function which will return the same HTML. This would then let me set it as a webmethod so that I can update that section of the page with jQuery, using the same code that generated the original html.

本质上,我试图用 C# 函数替换内联 ASP,该函数将返回相同的 HTML。这将让我将其设置为 webmethod,以便我可以使用 jQuery 更新页面的该部分,使用生成原始 html 的相同代码。

I really need some way of doing this, and this seems like a logical route.

我真的需要某种方式来做到这一点,这似乎是一条合乎逻辑的路线。

采纳答案by Keltex

You need to create a page in which you render the control:

您需要创建一个页面来呈现控件:

public static string RenderUserControl(string path)
{
    Page pageHolder = new Page();
    Control viewControl = pageHolder.LoadControl(path);

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

A user control on its own will not render. (This articlehas some related information from which I borrowed some code).

用户控件本身不会呈现。(本文有一些相关资料,我借用了一些代码)。

回答by JaCraig

I've not tried this, but you can load a control using the LoadControl function:

我没有试过这个,但是你可以使用 LoadControl 函数加载一个控件:

Control Example = LoadControl("~\Controls\MyControl.ascx");

Then you could try rendering the control:

然后你可以尝试渲染控件:

StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Example.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

But make sure that you override VerifyRenderingInServerForm and switch EnableEventValidation to false on the page.

但请确保您在页面上覆盖了 VerifyRenderingInServerForm 并将 EnableEventValidation 切换为 false。

Forget what I wrote above. I went back and tested and you can't call LoadControl because the webmethod is static. Since it's static you don't have a Page object to call... Which means no loading of user controls dynamically.

忘记我上面写的。我回去测试了,你不能调用 LoadControl,因为 webmethod 是静态的。由于它是静态的,因此您没有要调用的 Page 对象...这意味着不会动态加载用户控件。

回答by Tacoman667

This Explanationshould have the answers you are looking for. Basically you have to declare the control in the page then LoadControl in the codebehind.

这个解释应该有你正在寻找的答案。基本上,您必须在页面中声明控件,然后在代码隐藏中声明 LoadControl。

回答by Jules Colle

Keltex's answer was the only that worked for me since i was working with nested user-controls. Additionally, if you would want to pass parameters to your user control you can do so by:

自从我使用嵌套的用户控件以来,Keltex 的答案是唯一对我有用的答案。此外,如果您想将参数传递给您的用户控件,您可以这样做:

_MyNameSpace_MyUserControl viewcontrol = (_MyNameSpace_MyUserControl) pageHolder.LoadControl(path);

viewcontrol.MyParam = "My value";

回答by ali youhannaei

its very simple to use:

它的使用非常简单:

yourItem.Controls.Add(LoadControl("~/user control File path"));