在ASP.NET中,可以通过以下方式实现每次更改都进行构建的用户控件:
// MyUserControl.ascx.cs
using System;
using System.Web.UI;
namespace MyNamespace
{
public partial class MyUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// 控件逻辑代码
}
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="MyNamespace.MyUserControl" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyNamespace.MyPage" %>
My Page
[ToolboxData("<{0}:MyUserControl runat=server>{0}:MyUserControl>")]
,以便能够在工具箱中使用用户控件。// MyUserControl.ascx.cs
using System;
using System.Web.UI;
using System.ComponentModel;
namespace MyNamespace
{
[ToolboxData("<{0}:MyUserControl runat=server>{0}:MyUserControl>")]
public partial class MyUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// 控件逻辑代码
}
}
}
[ParseChildren(true)]
,以便能够在用户控件内包含其他控件。// MyUserControl.ascx.cs
using System;
using System.Web.UI;
using System.ComponentModel;
namespace MyNamespace
{
[ToolboxData("<{0}:MyUserControl runat=server>{0}:MyUserControl>")]
[ParseChildren(true)]
public partial class MyUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// 控件逻辑代码
}
}
}
这样,每次对用户控件的更改都会自动进行构建,并且可以在其他页面或控件中使用最新的用户控件。