[Implement day25 in ASP. NET control] customize the gridview field-Date Field

Source: Internet
Author: User

The first two articles introduce the drop-down list field (tbdropdownfield) used by the custom gridview, and learn more about how to inherit the boundfield category and rewrite the Custom field. Entering a date in the gridview is also a common requirement. In this article, we will implement another date field used by the gridview, and use the tbdateedit control in the field storage to edit the data.

Download program code: ASP. NET Server Control-day25.rar
Download the northwnd Database: northwnd.rar

 

1. Inheritance TbbaseboundfieldImplementation Tdatefield

The date field of the gridview needs to fasten the data. The general practice is to inherit from boundfield and rewrite it. However, we have inherited boundfield to create a custom field base category of tbbaseboundfield (see [Asp. net controls implement day23] customize the gridvie field category-implement tbdropdownfield field category). Therefore, the date field to be implemented inherits the name of tbbaseboundfield and overwrites the createfield method, returns a tdatefield object.

 

 

''' <Summary> ''' date delimiter. ''' </Summary> public class tbdatefieldinherits tbbaseboundfieldprotected overrides function createfield () as datacontrolfieldreturn new tbdatefield () end functionend class

 

The Custom Field category mainly involves overwriting the initializedatacell method to initialize the data storage, overwriting the ondatabindfield method to tie the field value to the boundfield object, and overwriting the extractvaluesfromcell method to retrieve the field value of the bucket, the following describes the methods to be overwritten.

 

Ii. Overwrite InitializedatacellMethod -Data storage lattice Initialization

First, override the initializedatacell method to process the initialization of the Data cell. When the read-only status is used, use the cell to present the data. If the status is edit, add the tbdateedit control to the cell, set the attributes of tbdatefield to the related attributes of the tbdateedit control. Then, the databinding event of the bucket (datacontrolfieldcell) or the date control (tdateedit) is directed to the ondatabindfield event processing method.

 

 

''' <Summary> ''' the resource storage is initialized. ''' </Summary> ''' <Param name = "cell"> the shard storage to be initialized. </Param> ''' <Param name = "rowstate"> the information column contains metadata. </Param> protected overrides sub initializedatacell (byval cell as datacontrolfieldcell, byval rowstate as datacontrolrowstate) dim odateedit as tbdateeditdim ocontrol as controlif me. cellisedit (rowstate) Then 'zookeeper zookeeper added tbdateedit control in the primary cell to control export odateedit = new tbdateedit () odateedit. firstdayofweek = me. firstdayofweekodateedit. showweeknumbers = me. showweeknumbersodateedit. calendarstyle = me. calendarstyleodateedit. lang = me. langodateedit. showtime = me. showtimeocontrol = odateeditcell. controls. add (ocontrol) elseocontrol = cellend ifif (ocontrol isnot nothing) andalso mybase. visible thenaddhandler ocontrol. databinding, new eventhandler (addressof me. ondatabindfield) end ifend sub

 

The tdateedit control is a date control developed by the author. For details about the tdateedit control, refer to the following articles in the author's blog.

Date control implementation teaching (1)-combined with JavaScript
Real-time teaching of date controls (2)-PostBack and events
Tbdateedit date control-version 1.0.0.0 (Open Source)

 

Iii. Overwrite OndatabindfieldMethod -Link the field value BoundfieldObject

When the gridview executes databind, The databinding event of each bucket is directed to the ondatabindfield method. In this method, we obtain the specified field value from the data source to format the field value, the field value is displayed on the cell or tdateedit control.

 

 

''' <Summary> ''' ends the bitwise value to the boundfield object. ''' </Summary> ''' <Param name = "sender"> controls metrics. </Param> ''' <Param name = "E"> event reference. </Param> protected overrides sub ondatabindfield (byval sender as object, byval e as eventargs) dim ocontrol as controldim odateedit as tbdateeditdim onamingcontainer as controldim odatavalue as object 'dim bencode as boolean' whether to encrypt dim stext as string' Format String ocontrol = directcast (sender, control) onamingcontainer = ocontrol. namingcontainerodatavalue = me. getvalue (onamingcontainer) bencode = (Me. supportshtmlencode andalso me. htmlencode) andalso typeof ocontrol is tablecell) stext = me. formatdatavalue (odatavalue, bencode) If typeof ocontrol is tablecell thenif (stext. length = 0) thenstext = "& nbsp;" End ifdirectcast (ocontrol, tablecell ). TEXT = stextelseif not typeof ocontrol is tbdateedit thenthrow new httpexception (string. format ("{0}: Wrong control type", me. datafield) end ifodateedit = directcast (ocontrol, tbdateedit) if me. applyformatineditmode thenodateedit. TEXT = stextelseif (not odatavalue is nothing) thenodateedit. TEXT = odatavalue. tostringend ifend sub

 

 

Iv. Overwrite ExtractvaluesfromcellMethod -Retrieve the field value of a bucket

When the client executes the update action after editing using the gridview, it calls the extractvaluesfromcell method to obtain the field value of the bucket for writing data to the data source. Therefore, we need to override the extractvaluesfromcell method to retrieve the value of the cell or tdateedit control and fill in the object with the iordereddictionary interface.

 

''' <Summary> ''' fill in the specified idictionary object with the value of the specified datacontrolfieldcell. ''' </Summary> ''' <Param name = "Dictionary"> is used to specify the value of the primary storage space. </Param> ''' <Param name = "cell"> contains the shard cell to be renewed. </Param> ''' <Param name = "rowstate"> specifies the condition of the data column. </Param> ''' <Param name = "includereadonly"> "true" indicates that the value contains the unique bit. Otherwise, the value is false. </Param> Public overrides sub extractvaluesfromcell (_ byval dictionary as iordereddictionary, _ byval cell as datacontrolfieldcell, _ byval rowstate as datacontrolrowstate, _ byval extends dereadonly as Boolean) dim ocontrol as control = nothingdim sdatafield as string = me. datafielddim ovalue as object = nothingdim snulldisplaytext as string = me. nulldisplaytextdim odateedit as tbdateeditif (rowstate and datacontrolrowstate. insert) = datacontrolrowstate. normal) orelse me. insertvisible) thenif (cell. controls. count> 0) thenocontrol = cell. controls. item (0) odateedit = trycast (ocontrol, tbdateedit) if (not odateedit is nothing) thenovalue = odateedit. textend ifelseif includereadonly thendim s as string = cell. textif (S = "& nbsp;") thenovalue = string. emptyelseif (Me. supportshtmlencode andalso me. htmlencode) thenovalue = httputility. htmldecode (s) elseovalue = Send ifend ifif (not ovalue is nothing) thenif typeof ovalue is string thenif (CSTR (ovalue ). length = 0) andalso me. convertemptystringtonull thenovalue = nothingelseif (CSTR (ovalue) = snulldisplaytext) andalso (snulldisplaytext. length> 0) thenovalue = nothingend ifend ifif dictionary. contains (sdatafield) thendictionary. item (sdatafield) = ovalueelsedictionary. add (sdatafield, ovalue) end ifend sub

 

V. Test Procedure

We use the data table employees of the northwnd database as an example to add a custom tbdatefield to the gridview to fasten the birthdate field. In addition, we add a read-only field of another boundfield and also tie the birthdate field for comparison.

 

 

            <bee:TBDateField DataField="BirthDate" HeaderText="BirthDate"SortExpression="BirthDate" DataFormatString="{0:d}"ApplyFormatInEditMode="True" CalendarStyle="Winter" /><asp:BoundField DataField="BirthDate" HeaderText="BirthDate"SortExpression="BirthDate" DataFormatString="{0:d}"ApplyFormatInEditMode="True" ReadOnly="true" />

 

Execute the program. When editing the data column, tbdatefield will be edited using the tdateedit control.

After you use tdateedit to edit the field value, press the "Update" button to write the data back to the database.

 

Note: This article is also published in "the first it state help tie Ren Competition". If you think this article is helpful to you, remember to repeat it to improve your popularity ^
Http://ithelp.ithome.com.tw/question/10013083
Http://ithelp.ithome.com.tw/question/10013091

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.