【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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 檢視一個通訊埠狀態
- GitLab 的元件狀態檢視Gitlab元件
- 檢視BW執行狀態
- WPF一個簡單的屬性編輯控制元件控制元件
- Kylin系統檢視firewalld狀態
- firewalld:檢視版本/幫助/狀態
- 一個可拖拽,移動,自由組合子控制元件的檢視控制元件,讓開發更簡單控制元件
- Flutter狀態管理Provider,簡單上手FlutterIDE
- vuex狀態管理簡單入門Vue
- 如何檢視CATIA許可證狀態
- 使用 telescope 檢視 schedule 執行狀態
- 系統狀態統計和檢視
- 在GI安裝完成後檢視叢集狀態時發現,磁碟組狀態不對
- 檢視mysql執行狀態的一些sqlMySql
- 簡簡單單的Vue3(外掛開發,路由系統,狀態管理)Vue路由
- Vuex 單狀態庫 與 多模組狀態庫Vue
- Elasticsearch 檢視節點狀態資訊(qbit)Elasticsearch
- 檢視使用 MySQL Shell 的連線狀態MySql
- cmd 檢視防火牆狀態以及關閉防火牆
- 3.4.4 檢視例項的靜默狀態
- linux檢視伺服器狀態命令--lsloadLinux伺服器
- mysql檢視主從同步狀態的方法MySql主從同步
- 通過Python檢視Azure VM的狀態Python
- ASP.NET Core如何禁用模型驗證(或者從模型狀態中移除某些屬性)?ASP.NET模型
- Flink狀態(一)
- Zustand 讓 React 狀態變得太簡單React
- 乙太網狀態檢視工具:Ethernet Status for MacMac
- GBase8s 資料庫檢視狀態資料庫
- uptime命令檢視Linux伺服器健康狀態Linux伺服器
- Flutter狀態管理Provider(一) 簡易使用FlutterIDE
- ASP.NET Core 應用程式狀態ASP.NET
- 檢查Capital許可狀態API
- Oracle SCN健康狀態檢查Oracle
- 檢測網路狀態 - flutterFlutter
- React Native 探索(三)元件的 Props (屬性) 和 State (狀態)React Native元件
- java實現用一個變數表示多個屬性的狀態Java變數
- 小程式全域性狀態管理
- 使用 ATX 判斷單選框選中狀態、開關狀態、圖示型別型別
- CentOS8檢視防火牆狀態,開啟/關閉防火牆CentOS防火牆