动态包含ASP.NET网页(Dynamic include with ASP.NET Web Pages)

编程入门 行业动态 更新时间:2024-10-28 08:17:13
动态包含ASP.NET网页(Dynamic include with ASP.NET Web Pages)

在ASP.NET Web Pages / Razor中有一种方法可以包含一个文件,比如Helper或类似的东西,而不知道它的名字?

在Web窗体中,我通过动态添加用户控件来执行此操作,如下面的代码,但我想知道是否有一种在Web页面中执行类似操作的方法?

ContentTemplateBase control = (ContentTemplateBase)Page.LoadControl("~/ContentTemplates/" + detail.Template + ".ascx"); control.DataSource = detail; plhTemplate.Controls.Add(control);

我知道我可以通过一堆条件语句来做到这一点,比如......

if (detail.Template== "Template1") { @MyHelpers.Template1(detail) } else if (detail.Template== "Template2") { @MyHelpers.Template2(detail) } ... etc

...但希望这样做而不必列出所有可能的选项。

In ASP.NET Web Pages/Razor is there a way to include a file, such as a Helper or something similar, without knowing it's name?

In Web Forms I do this by dynamically adding a User Control, like the code below, but am wondering if there is a way of doing something similar in Web Pages?

ContentTemplateBase control = (ContentTemplateBase)Page.LoadControl("~/ContentTemplates/" + detail.Template + ".ascx"); control.DataSource = detail; plhTemplate.Controls.Add(control);

I know I could do this with a bunch of conditional statements, like...

if (detail.Template== "Template1") { @MyHelpers.Template1(detail) } else if (detail.Template== "Template2") { @MyHelpers.Template2(detail) } ... etc

...but wanting to do this without having to list all possible options.

最满意答案

在重新阅读Web页面文档时,我看到我可以使用RenderPage ()方法来包含来自另一个文件的代码,所以这似乎是最好的选择。

On re-reading the Web Pages documentation I see I can use the RenderPage() method to include code from another file, so this seems to be the best option.

更多推荐

本文发布于:2023-04-28 08:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330941.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网页   动态   NET   ASP   Pages

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!