2

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

有谁知道如何更改您拖到 Web 表单上的所有 Web 用户控件的默认标记前缀?

4

2 回答 2

2

将用户控件添加到 Web 窗体页

你必须Register在下面的控件Page directive下面。

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

然后根据您的要求更改TagPrefixand 。TagName

例子

<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>
于 2012-04-18T03:09:15.253 回答
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>
于 2014-07-16T17:52:00.917 回答