【问题标题】:how to change default tagprefix in user control如何更改用户控件中的默认标记前缀
【发布时间】:2014-07-17 01:52:00
【问题描述】:

当您将 Web 用户控件拖到设计图面上时,它会自动分配 tagprefix = uc1。

有谁知道如何更改所有网络用户控件的默认标记前缀 你拖到一个网络表单上?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    Adding User Controls to a Web Forms Page

    你必须RegisterPage directive下方的控件,如下所示。

    <%@ Register TagPrefix="Guest" TagName="GuestExample" Src="~/YourControl.ascx" %>
    

    然后根据您的要求更改TagPrefixTagName

    例子

    <Guest:GuestExample ID="ID" runat="server" />
    


    不要在所有页面上复制它们,只需声明一次 在带有 web.config 文件的新 pages->controls 部分中 您的申请:

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <pages>
          <controls>
            <add tagPrefix="Guest" src="~/YourControl.ascx" tagName="GuestExample"/>
          </controls>
        </pages>
      </system.web>
    </configuration>
    

    【讨论】:

    • 这个方法也可以,但这不是我想要的。。我想在拖出用户控件时,它会自动分配我分配的标签前缀
    • 为了设置UserControlTagPrefixTagName。需要先注册控件。 Drag Drop WebUserControl 会将其转换为 Anchor 标签。
    • 如果每页不需要重复注册。使用Web.config 并在上面提到的所有页面中重复使用它。
    • 这是不可能的。我们只有几个选择。 1. Write the control registration in Web.config and another way is register the control in each page. Now choice is yours.
    【解决方案2】:

    实际上,这可以使用程序集级属性 TagPrefix 来实现。

    <Assembly: TagPrefix("MyCompany.Web", "SomeFancyTagPrefix")> 
    
    Public Class MyCustomControl
        Inherits WebControl
    
            'class implementation
    
    End Class
    

    第一个参数是控件的命名空间,第二个参数是你喜欢的标签前缀。

    将自定义控件拖放到页面上时的结果:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="MyCompany.Web.WebForm1" %>
    
    <%@ Register Assembly="My Company" Namespace="MyCompany.Web" TagPrefix="SomeFancyTagPrefix" %>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <SomeFancyTagPrefix:MyCustomControl ID="MyCustomControl1" runat="server">
            </SomeFancyTagPrefix:MyCustomControl>
        </div>
        </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多