日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

DataControlField與Parameter擴(kuò)展

系統(tǒng) 1911 0
一.為數(shù)據(jù)綁定控件(GridView)自定義列( DataControlField)

本來asp.net1.1中已經(jīng)存在DataGrid了,其中為我們提供了豐富的數(shù)據(jù)字段類型(即不同綁定列),如下代碼

< asp:DataGrid ID ="dg1" runat ="server" >
< Columns >
< asp:BoundColumn ></ asp:BoundColumn >
< asp:ButtonColumn ></ asp:ButtonColumn >
< asp:HyperLinkColumn ></ asp:HyperLinkColumn >
< asp:TemplateColumn ></ asp:TemplateColumn >
</ Columns >
</ asp:DataGrid >
網(wǎng)上也有介紹擴(kuò)展 DataGridColumn類的方法, asp.net2.0新增的GridView定義了全新的數(shù)據(jù)列,其也可以用于DetailsView,即 DataControlField類

來看下其內(nèi)置列的實現(xiàn)

DataControlField與Parameter擴(kuò)展

DataControlField類為數(shù)據(jù)列的基類,其派生類相信大家都很熟悉,如下圖

DataControlField與Parameter擴(kuò)展

當(dāng)然我們這里討論的不是怎么使用這些列,而是如何實現(xiàn)自定義列的過程.實現(xiàn)方法跟以前的有些相似,還是抽象類 DataControlField為我們實現(xiàn)了一些常用方法,并定義了一些必須讓我們?nèi)崿F(xiàn)的方法,讓字類去重寫.

下面列出常用相關(guān)的方法

這里以 自定義CalendarField 列為例

看DataControlField的 CloneField ()方法,先創(chuàng)建對象,再復(fù)制屬性
protected internal DataControlFieldCloneField()
{
DataControlFieldnewField
= this .CreateField();
this .CopyProperties(newField);
return newField;
}



1.創(chuàng)建列對象

DataControlField提供了 CopyProperties 方法,此工作一定要做

protected override DataControlFieldCreateField()
{
return new CalendarField();
}


2.復(fù)制屬性

DataControlField提供了 CopyProperties 方法

首先要先定義你需要的屬性,然后將屬性復(fù)制給CloneField方法中創(chuàng)建的對象,在更改屬性時要記得調(diào)用OnFieldChanged方法,通知 DataControlField 對象狀態(tài)發(fā)生變化,觸發(fā)FieldChanged事件


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> Customproperties #region Customproperties
// *******************************************************************
// PROPERTY:DataField
// Indicatesthefieldprovidingthedateinviewmode
public virtual string DataField
{
get
{
object o = this .ViewState[ " DataField " ];
if (o != null )
return ( string )o;
return String.Empty;
}

set
{
ViewState[
" DataField " ] = value;
OnFieldChanged();
}

}



// *******************************************************************
// PROPERTY:ReadOnly
// Indicatesthefieldfromwhichthetextofthedrop-downitemsistaken
public virtual bool ReadOnly
{
get
{
object o = base .ViewState[ " ReadOnly " ];
if (o != null )
return ( bool )o;
return false ;
}

set
{
base .ViewState[ " ReadOnly " ] = value;
OnFieldChanged();
}

}



// *******************************************************************
// PROPERTY:DataFormatString
// Indicatestheformatstringforthedate
public virtual string DataFormatString
{
get
{
object o = this .ViewState[ " DataFormatString " ];
if (o != null )
return ( string )o;
return String.Empty;
}

set
{
ViewState[
" DataFormatString " ] = value;
OnFieldChanged();
}

}


#endregion


// *******************************************************************
// METHOD:CopyProperties
//
protected override void CopyProperties(DataControlFieldnewField)
{
((CalendarField)newField).DataField
= this .DataField;
((CalendarField)newField).DataFormatString
= this .DataFormatString;
((CalendarField)newField).ReadOnly
= this .ReadOnly;

base .CopyProperties(newField);
}

3.初始化單元格狀態(tài)(InitializeCell方法)

即把你自己定義的東西添加到表格中。這里需要注意順序

我們知道GridView的單元格(即 DataControlCellType) 分為三種類型,頁眉,頁腳,數(shù)據(jù)項,到了確定是數(shù)據(jù)項以后,又要給 數(shù)據(jù)行 分類型

如編輯,插入,交替,選中等(即 DataControlRowState )

BoundField 在普通狀態(tài)下是文本,在編輯狀態(tài)下是TextBox.這里就是要做這個工作,在不同狀態(tài)下,加載不同控件。如下代碼

public override void InitializeCell(DataControlFieldCellcell,DataControlCellTypecellType,DataControlRowStaterowState, int rowIndex)
{

base .InitializeCell(cell,cellType,rowState,rowIndex);

// 如果為數(shù)據(jù)項
if (cellType == DataControlCellType.DataCell)
InitializeDataCell(cell,rowState);
}


protected virtual void InitializeDataCell(DataControlFieldCellcell,DataControlRowStaterowState)
{
Controlctrl
= null ;

DataControlRowStatestate
= rowState & DataControlRowState.Edit;

// 根據(jù)狀態(tài)加載不同控件
if (( ! ReadOnly && (state != DataControlRowState.Normal)) || rowState == DataControlRowState.Insert)
{
Calendarcal
= new Calendar();
cal.ToolTip
= this .HeaderText;
cell.Controls.Add(cal);

if ((DataField.Length != 0 ) &&
(DataField.Length
!= 0 ))
ctrl
= cal;

_inInsertMode
= rowState == DataControlRowState.Insert;
}

else if (DataField.Length != 0 )
{
ctrl
= cell;
}


// 給控件賦綁定的值
if ((ctrl != null ) && Visible)
{
ctrl.DataBinding
+= new EventHandler( this .OnBindingField);
}

}

4.實現(xiàn)數(shù)據(jù)交互 ( ExtractValuesFromCell方法)

第三步驟是顯示信息,這里則需要提取字段的值,然后添加到 dictionary集合中.具體其他操作暫且不管。

public override void ExtractValuesFromCell(IOrderedDictionarydictionary,DataControlFieldCellcell,DataControlRowStaterowState, bool includeReadOnly)
{
object selectedValue = null ;
if (cell.Controls.Count > 0 )
{
Calendarcal
= cell.Controls[ 0 ] as Calendar;

if (cal == null )
{
throw new InvalidOperationException( " CalendarFieldcouldnotextractcontrol. " );
}

else
selectedValue
= cal.SelectedDate;
}


// Addthevaluetothedictionary
if (dictionary.Contains(DataField))
dictionary[DataField]
= selectedValue;
else
dictionary.Add(DataField,selectedValue);
}

5.給列添加設(shè)計時支持

BoundField列在有字段綁定的情況下,在設(shè)計時顯示如下

DataControlField與Parameter擴(kuò)展

其是通過 GetDesignTimeValue 方法實現(xiàn)的,如可以這樣定義

protected virtual string GetDesignTimeValue()
{
return " <select><option>DataboundDate</option></select> " ;
}

在頁面效果如下

DataControlField與Parameter擴(kuò)展

好了,大致步驟就如此,你只需要熟悉上面方法,照著步驟做一遍就可以了.
另外還有DataGrid的DataGridColumn,如果你理解上面 DataControlField 的做法的話,DataGridColumn的實現(xiàn)關(guān)鍵方法則是 InitializeCell ,方法比較相似.但其沒有 ExtractValuesFromCell方法, 因為DataGrid當(dāng)時還沒有這么的智能化的自動編輯功能 .

二.數(shù)據(jù)源控件控件自定義參數(shù)

數(shù)據(jù)源控件我們也比較常用,所以要學(xué)習(xí)下如何自定義參數(shù),如下圖,為內(nèi)置已經(jīng)實現(xiàn)的幾個參數(shù)類.

DataControlField與Parameter擴(kuò)展

我們還是關(guān)注下 Parameter類,其主要提供了一個空的 Evaluate 方法給,派生類需要實現(xiàn)此方法返回經(jīng)過更新的值.其實現(xiàn)比較簡單,來看下其內(nèi)部 QueryStringParameter 的實現(xiàn)過程.應(yīng)該說沒什么難度.抓住重點就好


public class QueryStringParameter:Parameter
{
public QueryStringParameter()
{
}


protected QueryStringParameter(QueryStringParameteroriginal): base (original)
{
this .QueryStringField = original.QueryStringField;
}


public QueryStringParameter( string name, string queryStringField): base (name)
{
this .QueryStringField = queryStringField;
}


public QueryStringParameter( string name,TypeCodetype, string queryStringField): base (name,type)
{
this .QueryStringField = queryStringField;
}


protected override ParameterClone()
{
return new QueryStringParameter( this );
}


protected override object<
分享到:
評論

DataControlField與Parameter擴(kuò)展


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 盐城市| 象山县| 旌德县| 宣恩县| 石林| 浦东新区| 莫力| 清流县| 平顺县| 酒泉市| 苍梧县| 五莲县| 石楼县| 海口市| 青州市| 平乡县| 定陶县| 澎湖县| 静安区| 南开区| 大丰市| 确山县| 永仁县| 乌审旗| 定远县| 仙桃市| 黄大仙区| 房山区| 孝义市| 开阳县| 昭平县| 古蔺县| 高州市| 平阳县| 济源市| 福安市| 巢湖市| 精河县| 河南省| 武功县| 二连浩特市|