【发布时间】:2014-07-17 01:52:00
【问题描述】:
当您将 Web 用户控件拖到设计图面上时,它会自动分配 tagprefix = uc1。
有谁知道如何更改所有网络用户控件的默认标记前缀 你拖到一个网络表单上?
【问题讨论】:
标签: asp.net
当您将 Web 用户控件拖到设计图面上时,它会自动分配 tagprefix = uc1。
有谁知道如何更改所有网络用户控件的默认标记前缀 你拖到一个网络表单上?
【问题讨论】:
标签: asp.net
Adding User Controls to a Web Forms Page
你必须RegisterPage directive下方的控件,如下所示。
<%@ Register TagPrefix="Guest" TagName="GuestExample" Src="~/YourControl.ascx" %>
然后根据您的要求更改TagPrefix 和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>
【讨论】:
UserControl的TagPrefix和TagName。需要先注册控件。 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.
实际上,这可以使用程序集级属性 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>
【讨论】: