DataControlField与Parameter扩展

       示例代码请参考 此篇


一.为数据绑定控件(GridView)自定义列( DataControlField)

本来asp.net1.1中已经存在DataGrid了,其中为我们提供了丰富的数据字段类型(即不同绑定列),如下代码

None.gif              < asp:DataGrid  ID ="dg1"  runat ="server" >
None.gif            
< Columns >
None.gif            
< asp:BoundColumn ></ asp:BoundColumn >
None.gif            
< asp:ButtonColumn ></ asp:ButtonColumn >
None.gif            
< asp:HyperLinkColumn ></ asp:HyperLinkColumn >
None.gif            
< asp:TemplateColumn ></ asp:TemplateColumn >
None.gif            
</ Columns >
None.gif            
</ asp:DataGrid >
网上也有介绍扩展 DataGridColumn类的方法,asp.net2.0新增的GridView定义了全新的数据列,其也可以用于DetailsView,即 DataControlField类

来看下其内置列的实现



DataControlField类为数据列的基类,其派生类相信大家都很熟悉,如下图



当然我们这里讨论的不是怎么使用这些列,而是如何实现自定义列的过程.实现方法跟以前的有些相似,还是抽象类DataControlField为我们实现了一些常用方法,并定义了一些必须让我们去实现的方法,让字类去重写.

下面列出常用相关的方法

这里以自定义CalendarField列为例

看DataControlField的CloneField()方法,先创建对象,再复制属性
None.gif      protected   internal  DataControlField CloneField()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        DataControlField newField 
= this.CreateField();
InBlock.gif        
this.CopyProperties(newField);
InBlock.gif        
return newField;
ExpandedBlockEnd.gif    }

None.gif


1.创建列对象

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

None.gif          protected   override  DataControlField CreateField()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return new CalendarField();
ExpandedBlockEnd.gif        }


2.复制属性

DataControlField提供了CopyProperties 方法

首先要先定义你需要的属性,然后将属性复制给CloneField方法中创建的对象,在更改属性时要记得调用OnFieldChanged方法,通知DataControlField对象状态发生变化,触发FieldChanged事件

ContractedBlock.gif ExpandedBlockStart.gif
ContractedBlock.gifExpandedBlockStart.gif        Custom properties#region Custom properties
InBlock.gif        
// *******************************************************************
InBlock.gif        
// PROPERTY: DataField
InBlock.gif        
// Indicates the field providing the date in view mode
InBlock.gif
        public virtual string DataField
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object o = this.ViewState["DataField"];
InBlock.gif                
if (o != null)
InBlock.gif                    
return (string) o;
InBlock.gif                
return String.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"DataField"= value;
InBlock.gif                OnFieldChanged();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
// *******************************************************************
InBlock.gif        
// PROPERTY: ReadOnly
InBlock.gif        
// Indicates the field from which the text of the drop-down items is taken
InBlock.gif
        public virtual bool ReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object o = base.ViewState["ReadOnly"];
InBlock.gif                
if (o != null)
InBlock.gif                    
return (bool) o;
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.ViewState["ReadOnly"= value;
InBlock.gif                OnFieldChanged();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
// *******************************************************************
InBlock.gif        
// PROPERTY: DataFormatString
InBlock.gif        
// Indicates the format string for the date 
InBlock.gif
        public virtual string DataFormatString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
object o = this.ViewState["DataFormatString"];
InBlock.gif                
if (o != null)
InBlock.gif                    
return (string)o;
InBlock.gif                
return String.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"DataFormatString"= value;
InBlock.gif                OnFieldChanged();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif        
#endregion

None.gif
None.gif        
// *******************************************************************
None.gif        
// METHOD: CopyProperties
None.gif        
//  
None.gif
        protected override void CopyProperties(DataControlField newField)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            ((CalendarField)newField).DataField 
= this.DataField;
InBlock.gif            ((CalendarField)newField).DataFormatString 
= this.DataFormatString;
InBlock.gif            ((CalendarField)newField).ReadOnly 
= this.ReadOnly;
InBlock.gif
InBlock.gif            
base.CopyProperties(newField);
ExpandedBlockEnd.gif        }

3.初始化单元格状态(InitializeCell方法)

即把你自己定义的东西添加到表格中。这里需要注意顺序

我们知道GridView的单元格(即 DataControlCellType)分为三种类型,页眉,页脚,数据项,到了确定是数据项以后,又要给 数据行分类型

如编辑,插入,交替,选中等(即 DataControlRowState)

BoundField在普通状态下是文本,在编辑状态下是TextBox.这里就是要做这个工作,在不同状态下,加载不同控件。如下代码

None.gif        public   override   void  InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState,  int  rowIndex)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            
base.InitializeCell(cell, cellType, rowState, rowIndex);
InBlock.gif
InBlock.gif            
//如果为数据项
InBlock.gif
            if (cellType == DataControlCellType.DataCell)
InBlock.gif                InitializeDataCell(cell, rowState);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
protected   virtual   void  InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Control ctrl 
= null;
InBlock.gif
InBlock.gif            DataControlRowState state 
= rowState & DataControlRowState.Edit;
InBlock.gif
InBlock.gif            
//根据状态加载不同控件
InBlock.gif
            if ((!ReadOnly && (state != DataControlRowState.Normal)) || rowState == DataControlRowState.Insert)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Calendar cal 
= new Calendar();
InBlock.gif                cal.ToolTip 
= this.HeaderText;
InBlock.gif                cell.Controls.Add(cal);
InBlock.gif
InBlock.gif                
if ((DataField.Length != 0&&
InBlock.gif                    (DataField.Length 
!= 0))
InBlock.gif                    ctrl 
= cal;
InBlock.gif
InBlock.gif                _inInsertMode 
= rowState == DataControlRowState.Insert;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (DataField.Length != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ctrl 
= cell;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//给控件赋绑定的值
InBlock.gif
            if ((ctrl != null&& Visible)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ctrl.DataBinding 
+= new EventHandler(this.OnBindingField);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

4.实现数据交互( ExtractValuesFromCell方法)

第三步骤是显示信息,这里则需要提取字段的值,然后添加到 dictionary集合中.具体其他操作暂且不管。

None.gif          public   override   void  ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState,  bool  includeReadOnly)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
object selectedValue = null;
InBlock.gif            
if (cell.Controls.Count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Calendar cal 
= cell.Controls[0as Calendar;
InBlock.gif
InBlock.gif                
if (cal == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new InvalidOperationException("CalendarField could not extract control.");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
InBlock.gif                    selectedValue 
= cal.SelectedDate;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Add the value to the dictionary
InBlock.gif
            if (dictionary.Contains(DataField))
InBlock.gif                dictionary[DataField] 
= selectedValue;
InBlock.gif            
else
InBlock.gif                dictionary.Add(DataField, selectedValue);
ExpandedBlockEnd.gif        }

5.给列添加设计时支持

BoundField列在有字段绑定的情况下,在设计时显示如下



其是通过 GetDesignTimeValue方法实现的,如可以这样定义

None.gif          protected   virtual   string  GetDesignTimeValue()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return "<select><option>Databound Date</option></select>";
ExpandedBlockEnd.gif        }

在页面效果如下

 

好了,大致步骤就如此,你只需要熟悉上面方法,照着步骤做一遍就可以了.
另外还有DataGrid的DataGridColumn,如果你理解上面 DataControlField的做法的话,DataGridColumn的实现关键方法则是 InitializeCell,方法比较相似.但其没有 ExtractValuesFromCell方法,因为DataGrid当时还没有这么的智能化的自动编辑功能.

二.数据源控件控件自定义参数

数据源控件我们也比较常用,所以要学习下如何自定义参数,如下图,为内置已经实现的几个参数类.



我们还是关注下Parameter类,其主要提供了一个空的Evaluate 方法给,派生类需要实现此方法返回经过更新的值.其实现比较简单,来看下其内部QueryStringParameter的实现过程.应该说没什么难度.抓住重点就好


None.gif public   class  QueryStringParameter : Parameter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif        
public QueryStringParameter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected QueryStringParameter(QueryStringParameter original) : base(original)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.QueryStringField = original.QueryStringField;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public QueryStringParameter(string name, string queryStringField) : base(name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.QueryStringField = queryStringField;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public QueryStringParameter(string name, TypeCode type, string queryStringField) : base(name, type)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.QueryStringField = queryStringField;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected override Parameter Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return new QueryStringParameter(this);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected override object Evaluate(HttpContext context, Control control)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ((context != null&& (context.Request != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return context.Request.QueryString[this.QueryStringField];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif        
public string QueryStringField
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object obj2 = base.ViewState["QueryStringField"];
InBlock.gif            
if (obj2 == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return string.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return (string) obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this.QueryStringField != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.ViewState["QueryStringField"= value;
InBlock.gif                
base.OnParameterChanged();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


三.属性集合

以上的
DataControlField Parameter 都具有集合类型 DataControlFieldCollectionParameterCollection

有时候扩展了以后可能还需要更改集合类型,因为有时候集合类型是在开发人员在功能已经实现的情况下编写的.现在的扩展势必需要发生一些改变.而属性类型一更改,控件内部的集合属性也需要更改.当然偷懒的话就不改了,这么一改比较麻烦.但这种事情还是会碰到的.这里就提一下.

好了就这么写一下,希望对大家有帮助.本人已经在上海的某家公司工作.
今后的工作大多会更多地设计到控件的开发上,希望与大家多交流
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值