简体   繁体   English

在现有的ASP.NET WebForms应用程序中使用WebComponents

[英]Use webcomponents in existing ASP.NET WebForms application

I have an large existing ASP.NET WebForms application. 我有一个大型的现有ASP.NET WebForms应用程序。 This application has a masterpage who manager security, navigation, ... Pages developped are in a ContentPlaceHolder. 此应用程序有一个主页,负责管理安全性,导航,...开发的页面位于ContentPlaceHolder中。

I would like to develop new pages in JS with web components. 我想使用Web组件在JS中开发新页面。 To use a web component on a page, it must be imported ( <link rel="import" href="..."> ). 要在页面上使用Web组件,必须将其导入( <link rel="import" href="..."> )。 This import must be on the <head> part of the page. 此导入必须在页面的<head>部分中。 In my case, the <head> is on the master page. 就我而言, <head>在母版页上。 So I have to import all the existing web components in the master page to be able to use them. 因此,我必须在母版页中导入所有现有的Web组件才能使用它们。

It does not sound like a very good idea. 听起来这不是一个好主意。

How can I do it otherwise ? 否则我该怎么办?

Asp.Net lets you place ContentPlaceHolder in head tag. Asp.Net允许您将ContentPlaceHolder放置在head标签中。 You can load the web components required by the page instead of loading them all in Master Page. 您可以加载页面所需的Web组件,而不是全部加载到“母版页”中。

Master Page 母版页

<html>
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="cpHead" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="cpBody" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Web Form 网络表格

<asp:Content ID="Content1" ContentPlaceHolderID="cpHead" runat="server">
    <link rel="import" href="...">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpBody" runat="server">
</asp:Content>

Your HTML will look like 您的HTML看起来像

<html>
<head>
    <title></title>
    <link href="..." rel="import">
</head>
<body>
</body>
</html>

You would normally use RegisterClientScriptInclude . 您通常会使用RegisterClientScriptInclude This will add the script file to the top of the page, but not in the <head> . 这会将脚本文件添加到页面顶部,而不是<head>

ClientScript.RegisterClientScriptInclude("myScript", "myJavaScriptFile.js");

If you really want it in the <head> you need to use a HtmlGenericControl 如果您确实要在<head>中使用它,则需要使用HtmlGenericControl

HtmlGenericControl genericControl = new HtmlGenericControl("script");
genericControl.Attributes.Add("type", "text/javascript");
genericControl.Attributes.Add("src", "myJavaScriptFile.js");
this.Page.Header.Controls.Add(genericControl);

It does not matter where you place this code (Master, Page, User Control). 放置此代码的位置(母版,页面,用户控件)无关紧要。 Aspnet will ensure they end up in the right place client side. Aspnet将确保它们最终出现在客户端的正确位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM