如果專案中有N頁會用到該控制項,就要註冊N次顯得有些麻煩。有沒有註冊一次就可以讓所有頁面使用的方式?
有。
請在Web.config中<system.web> -> <pages> -> <controls>加入如下節點
<add tagPrefix="MyTag" namespace="MyWorks.WebControls" assembly="MyWorks"/>
tagPrefix為控制項加入頁面時的前置詞,assembly為組件名稱,namespace為命名空間名稱。
<add tagPrefix="MyTag" namespace="MyWorks.WebControls" assembly="MyWorks"/>
Protected Overrides Function CreateField() As System.Web.UI.WebControls.DataControlField Return New SerialNumberField() End Function5.覆寫InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControls.DataControlCellType, ByVal rowState As System.Web.UI.WebControls.DataControlRowState, ByVal rowIndex As Integer),如下
Public Overrides Sub InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControls.DataControlCellType, ByVal rowState As System.Web.UI.WebControls.DataControlRowState, ByVal rowIndex As Integer) MyBase.InitializeCell(cell, cellType, rowState, rowIndex) If cellType = DataControlCellType.DataCell Then cell.Text = (rowIndex + 1).ToString() End If6.建置專案End Sub
<%@ Register assembly="SerialNumberField" namespace="SerialNumberField" tagprefix="sn" %>3.於GridView的<Columns></Columns>中加入SerialNumberField(如同BoundField一樣),如下
<sn:SerialNumberField />
Protected Enum Family As Integer Grandpa = 0 Grandma = 1 Father = 2 Mother = 3 Brother = 4 Sister = 5 End Enum
'列出列舉成員名稱 Me.Response.Write("列出列舉成員名稱<br />") For Each str As String In [Enum].GetNames(GetType(Family)) Me.Response.Write(str) Me.Response.Write("<br />") Next Me.Response.Write("<p></p>") '列出列舉值 Me.Response.Write("列出列舉值<br />") For Each value As Integer In [Enum].GetValues(GetType(Family)) Me.Response.Write(value.ToString()) Me.Response.Write("<br />") Next Me.Response.Write("<p></p>") '經由列舉值取得列舉成員名稱 Me.Response.Write("經由列舉值取得列舉成員名稱<br />") For Each value As Integer In [Enum].GetValues(GetType(Family)) Me.Response.Write("值:" & value.ToString() & " 名稱:" & [Enum].GetName(GetType(Family), value)) Me.Response.Write("<br />") Next