作Asp.net的开发的都知道,从DataGrid到GridView,都支持Template Column,功能是在太强了,可以说没有做不到,只有想不到!可是如果功能强大了必然意味着用起来麻烦,可是大多数的时候,BoundField都是不能满足要求的,你会说用TemplateField啊,没错,用模板列确实可以轻易的解决问题,可是你看下面代码:
< asp:CommandField ShowEditButton ="True" ShowDeleteButton ="True" />
< asp:BoundField DataField ="DataId" HeaderText ="DataId" ReadOnly ="True" SortExpression ="DataId"
Visible ="False" InsertVisible ="False" />
< asp:TemplateField HeaderText ="编码" SortExpression ="ClientId" >
< edititemtemplate >
< asp:TextBox runat ="server" Text ='<%# Bind("ClientId") % > ' MaxLength="4" columns="4" onfocus="this.select();" id="TextBox1"> </ asp:TextBox >
</ edititemtemplate >
< itemtemplate >
< asp:Label runat ="server" Text ='<%# Bind("ClientId") % > ' id="Label1"> </ asp:Label >
</ itemtemplate >
</ asp:TemplateField >
< asp:TemplateField HeaderText ="说明" SortExpression ="ClientName" >
< edititemtemplate >
< asp:TextBox runat ="server" Text ='<%# Bind("ClientName") % > ' MaxLength="20" columns="20" id="TextBox2"> </ asp:TextBox >
</ edititemtemplate >
< itemtemplate >
< asp:Label runat ="server" Text ='<%# Bind("ClientName") % > ' id="Label2"> </ asp:Label >
</ itemtemplate >
</ asp:TemplateField >
</ Columns >
这只是为了实现限制输入宽度〔否则默认用BoundField在编辑状态下,是在是太宽了,破坏了界面的布局〕,就使用了模板列。这还只是一个简单的表的编辑,只有3个字段,如果是一个几十个字段的表,包含各种类型的字段输入,要编辑起来还真是麻烦,而且有很多重复性的工作,这简直就是简单枯燥的体力劳动!
穷则思变,经过参考一些资料,我把一些常用的字段类型封装成了自定义的DataField,然后直接在GridView中使用,您还别说,2005的代码提示还真不是盖的,对自定义的DataField的支持非常的好(只有一个缺点:不能使用Columns的那个设计界面了,只能改标记代码),您在看看下面这段:
< yh:TextField DataField ="DataId" HeaderText ="DataId" InsertVisible ="False" ReadOnly ="True" SortExpression ="DataId" Visible ="False" />
< yh:DictionaryField HeaderText ="分公司" DataField ="SaleCoId" SortExpression ="SaleCoId" Tables ="td_saleco" ValueFields ="salecoid" DisplayFields ="SaleCoName" />
< yh:DateTimeField HeaderText ="发货日期" DataField ="SendDate" SortExpression ="SendDate" ReadOnly ="True" ShowTime ="False" UseLongFormat ="True" />
< yh:NumericField HeaderText ="Fc" DataField ="TestFC" SortExpression ="TestFC" InsertVisible ="False" Visible ="False" />
< yh:DictionaryField HeaderText ="客户" DataField ="CustomerId" SortExpression ="CustomerId" ValueFields ="CustomerId" DisplayFields ="CustomerName" Tables ="Td_Customer" />
< yh:StateField HeaderText ="是否内销" DataField ="InteriorFlag" SortExpression ="InteriorFlag" KeyValuePairs ="0:外销,1:内销" />
</ columns >
怎么样,是不是觉得清爽了很多!
至于怎么样实现,这可是我公司的版权不能外传! 对了,请你参考这段文章Custom Data Control Fields,人家比我说得清楚多了,我就是从那学来的:)