5月
31
2006
分類:
最近更新:
2006-05-31
ASP.Net~~自定控制項的 Property
ASP.Net:: 自定控制項的 Property
在自訂控制項類別中,定義 public property 成員,則在 ASPX 頁面標籤中,就可使用相同名稱的標籤屬性(tag attribute)。 ASP.Net 會在 Page_Load() 之前就調用自訂控制項的 property set method ,設定控制項的屬性值。
// See also: Visual Studio 2003 .Net Sample Source Code, Duwamish7
//在 modules/viewsourcemodule.ascx.cs 定義 public property 'SourceUrl'
namespace Duwamish7.Web
{
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel;
using System.Data;
public class ViewSourceModule : ModuleBase
{
protected System.Web.UI.WebControls.HyperLink SourceHyperLink;
protected void Page_Load(Object sender, EventArgs e)
{
}
/// <value>
/// Property SourceUrl is used to set the URL of the hyperlink for source viewing.
/// </value>
public String SourceUrl
{
set
{
SourceHyperLink.NavigateUrl = PageBase.UrlBase + "/" + value;
}
}
} // class ViewSourceModule
} // namespace Duwamish7.Web
<%@ Page Language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="Duwamish7.Web.Default" EnableSessionState="true" %>
<%@ Register TagPrefix="Module" TagName="ViewSource" Src="modules/viewsourcemodule.ascx" %>
<Module:ViewSource id="ModuleViewSource"
SourceUrl="viewsource.aspx?path=default.src"
runat="server">
</Module:ViewSource>
注意,如果自定控制項的 property 內容有使用到執行時期,例如 Page_Load() 中才建構的實例,那麼在頁面中設定標籤屬性的用法,將發生執行時期的例外。因為 ASP.Net 會在 Page_Load() 之前就先呼叫控制項的 property set method。
樂多舊網址: http://blog.roodo.com/rocksaying/archives/1690535.html