【dinghao】asp.net控制元件開發(一)簡單屬性、檢視狀態、控制元件狀態
瞭解控制元件的生命週期:
其中Init是由內而外,即先子控制元件後父控制元件,Load等相反。
在Init之前控制元件樹根據宣告語法已經生成
控制元件狀態和檢視狀態:
控制元件狀態是專門為維護控制元件的核心行為功能而設計的,而 ViewState 只包含維護控制元件內容 (UI) 的狀態。應該按照這個規則設計控制元件狀態。
先看一個例子:改自asp伺服器控制元件和元件開發一書
PageTracker,用來跟蹤頁面會話狀態和程式狀態收到的點選數目,並計算頁面往返時間
跟蹤方式:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public enum PageTrackingMode
{
ByApplication,
BySession,
ByTripTime
}控制元件程式碼:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> [
DefaultProperty("TrackingMode")
]
public class PageTracker : WebControl {
private TimeSpan _tripTime;
[
Category("Appearance"),
DefaultValue("{0}"),
Description("The formatting string used to display the value being tracked.")
]
public virtual string FormatString {
get {
string s = (string)ViewState["FormatString"];
return ((s == null) ? "{0}" : s);
}
set {
ViewState["FormatString"] = value;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public int Hits {
get {
PageTrackingMode mode = TrackingMode;
object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}
else {
throw new NotSupportedException("Hits is only supported when TrackingMode is PageTrackingMode.ByApplication or PageTrackingMode.BySession");
}
return ((o == null) ? 0 : (int)o);
}
}
[
Category("Behavior"),
DefaultValue(PageTrackingMode.ByApplication),
Description("The type of tracking to perform.")
]
public virtual PageTrackingMode TrackingMode {
get {
object mode = ViewState["TrackingMode"];
return ((mode == null) ? PageTrackingMode.ByApplication : (PageTrackingMode)mode);
}
set {
if (value < PageTrackingMode.ByApplication || value > PageTrackingMode.ByTripTime) {
throw new ArgumentOutOfRangeException("value");
}
ViewState["TrackingMode"] = value;
// Perform cleanup of the old storage.
// We have to check that the Page is not null
// because the control could be initialized
// before it is added to the control tree.
// 因為 Application and Session
// 在設計時不可用,所以會出現異常
switch (TrackingMode) {
case PageTrackingMode.ByApplication:
if (Page != null && Page.Application != null) {
Page.Application.Remove(HitsKey);
}
break;
case PageTrackingMode.BySession:
if (Page != null && Page.Session != null) {
Page.Session.Remove(HitsKey);
}
break;
case PageTrackingMode.ByTripTime:
ViewState.Remove("TimeStamp");
break;
}
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public TimeSpan TripTime {
get {
if (TrackingMode != PageTrackingMode.ByTripTime) {
throw new NotSupportedException("TripTime is only supported when TrackingMode is PageTrackingMode.ByTripTime");
}
return _tripTime;
}
}
protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Div;
}
}
private string HitsKey {
get {
// Create a unique HitsKey for the page for keying
// in hits per page in the Application and Session objects.
return Page.GetType().FullName;
}
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
switch (TrackingMode){
case PageTrackingMode.ByApplication:
// Update the page hits in the Application
// object. This operation needs a lock because
// multiple users could access a page at the same
// time.
其中Init是由內而外,即先子控制元件後父控制元件,Load等相反。
在Init之前控制元件樹根據宣告語法已經生成
控制元件狀態和檢視狀態:
控制元件狀態是專門為維護控制元件的核心行為功能而設計的,而 ViewState 只包含維護控制元件內容 (UI) 的狀態。應該按照這個規則設計控制元件狀態。
先看一個例子:改自asp伺服器控制元件和元件開發一書
PageTracker,用來跟蹤頁面會話狀態和程式狀態收到的點選數目,並計算頁面往返時間
跟蹤方式:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public enum PageTrackingMode
{
ByApplication,
BySession,
ByTripTime
}
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> [
DefaultProperty("TrackingMode")
]
public class PageTracker : WebControl {
private TimeSpan _tripTime;
[
Category("Appearance"),
DefaultValue("{0}"),
Description("The formatting string used to display the value being tracked.")
]
public virtual string FormatString {
get {
string s = (string)ViewState["FormatString"];
return ((s == null) ? "{0}" : s);
}
set {
ViewState["FormatString"] = value;
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public int Hits {
get {
PageTrackingMode mode = TrackingMode;
object o = null;
if (mode == PageTrackingMode.ByApplication) {
o = Page.Application[HitsKey];
}
else if (mode == PageTrackingMode.BySession) {
o = Page.Session[HitsKey];
}
else {
throw new NotSupportedException("Hits is only supported when TrackingMode is PageTrackingMode.ByApplication or PageTrackingMode.BySession");
}
return ((o == null) ? 0 : (int)o);
}
}
[
Category("Behavior"),
DefaultValue(PageTrackingMode.ByApplication),
Description("The type of tracking to perform.")
]
public virtual PageTrackingMode TrackingMode {
get {
object mode = ViewState["TrackingMode"];
return ((mode == null) ? PageTrackingMode.ByApplication : (PageTrackingMode)mode);
}
set {
if (value < PageTrackingMode.ByApplication || value > PageTrackingMode.ByTripTime) {
throw new ArgumentOutOfRangeException("value");
}
ViewState["TrackingMode"] = value;
// Perform cleanup of the old storage.
// We have to check that the Page is not null
// because the control could be initialized
// before it is added to the control tree.
// 因為 Application and Session
// 在設計時不可用,所以會出現異常
switch (TrackingMode) {
case PageTrackingMode.ByApplication:
if (Page != null && Page.Application != null) {
Page.Application.Remove(HitsKey);
}
break;
case PageTrackingMode.BySession:
if (Page != null && Page.Session != null) {
Page.Session.Remove(HitsKey);
}
break;
case PageTrackingMode.ByTripTime:
ViewState.Remove("TimeStamp");
break;
}
}
}
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public TimeSpan TripTime {
get {
if (TrackingMode != PageTrackingMode.ByTripTime) {
throw new NotSupportedException("TripTime is only supported when TrackingMode is PageTrackingMode.ByTripTime");
}
return _tripTime;
}
}
protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Div;
}
}
private string HitsKey {
get {
// Create a unique HitsKey for the page for keying
// in hits per page in the Application and Session objects.
return Page.GetType().FullName;
}
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
switch (TrackingMode){
case PageTrackingMode.ByApplication:
// Update the page hits in the Application
// object. This operation needs a lock because
// multiple users could access a page at the same
// time.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-332340/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [React]屬性和狀態React
- 檢視一個通訊埠狀態
- 檢視BW執行狀態
- 檢視看防火牆狀態防火牆
- c#之statusstrip狀態列控制元件(1)C#控制元件
- 一個簡單的狀態列示例
- 基於COM和.Net檢視狀態的Asp.netASP.NET
- Linux下用netstat檢視網路狀態、埠狀態Linux
- 系統狀態檢視工具Sysstat
- firewalld:檢視版本/幫助/狀態
- GitLab 的元件狀態檢視Gitlab元件
- 網站狀態驗證WebBrowser控制元件實現網站Web控制元件
- Asp.net開發之旅--動態產生控制元件ASP.NET控制元件
- WPF一個簡單的屬性編輯控制元件控制元件
- 一個可拖拽,移動,自由組合子控制元件的檢視控制元件,讓開發更簡單控制元件
- vuex狀態管理簡單入門Vue
- 使用 telescope 檢視 schedule 執行狀態
- 系統狀態統計和檢視
- 檢視映象資料庫的狀態資料庫
- linux perl 檢視檔案狀態Linux
- 系統狀態檢視工具systat(轉)
- Kylin系統檢視firewalld狀態
- 在狀態列中插入類似進度條的可視控制元件控制元件
- 在GI安裝完成後檢視叢集狀態時發現,磁碟組狀態不對
- MFC在狀態列中使用進度條控制元件控制元件
- Vuex 單狀態庫 與 多模組狀態庫Vue
- 檢視mysql執行狀態的一些sqlMySql
- 【Clingingboy】asp.net 簡單介紹自定義控制元件簡單屬性和複雜屬性ASP.NET控制元件
- 簡簡單單的Vue3(外掛開發,路由系統,狀態管理)Vue路由
- Flutter狀態管理Provider,簡單上手FlutterIDE
- mysql檢視主從同步狀態的方法MySql主從同步
- Linux 檢視網路連線狀態Linux
- Solaris之檢視執行系統狀態
- 【主機】檢視伺服器埠狀態伺服器
- OracleRAC管理 之 叢集狀態&資訊檢視Oracle
- cmd 檢視防火牆狀態以及關閉防火牆
- 檢視使用 MySQL Shell 的連線狀態MySql
- 爛筆頭——Oracle檢視資料庫開啟狀態Oracle資料庫