2012-04-18 92 views

回答

2

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" /> 

enter image description here


,而不是复制他们的所有网页,只需申报一次即可 新pages-内>控制与连接段.config文件的 您的应用程序:

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

此方法也可以工作,但这不是我想要的..我想要拖出用户控件时,它会自动分配我分配的tagprefix – user1328360 2012-04-18 03:43:21

+0

为了设置'UserControl'的TagPrefix和TagName。首先需要注册控件。 '拖放'WebUserControl'会将其转换为'Anchor'标签。 – Pankaj 2012-04-18 05:14:37

+1

该方法是手动更改的 – user1328360 2012-04-18 07:09:19

2

实际上,这可以使用Assembly-Level属性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>