Application of the seven field types (fields type) of the gridview Data Control

Source: Internet
Author: User

Gridview supports a total of seven field types. The field should have been called "column", but ASP. NET 2.0 uses another name, "field". It is not intuitive to name the priest, because unknown people do not know what field represents, but since ASP. NET 2.0 uses the field, you can accommodate it. Table 9-6 shows that the gridview supports seven field types.

Table 9-6 gridview field type table

Field field type

Description

Boundfield)

Display the field data of the data source in text format.

Buttonfield)

Display the command button in the data binding control. Depending on the control, it allows you to display data rows or columns with Custom button controls (such as the add or remove button). When you press the button, a rowcommand event is triggered.

Commandfield)

Display the button containing the command, including the select, edit, update, and delete commands. (The commandfield of detailsview supports the INSERT command)

Checkboxfield (checkbox field)

Displayed as a checkbox type, usually used for displaying a Boolean value of true/false

Hyperlinkfield)

Display the data source field data as a hyperlink and specify another navigateurl hyperlink.

Imagefield)

Display image fields in the data binding control

Templatefield)

Display custom template content

The field declaration in the gridview is included in the <columns>... </columns> In the tag block, see the HTML program after Example 9-1 is completed. The following is the <columns> block declaration:

<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" datakeynames = "employeeid" ceceid = "sqldatasource1" emptydatatext = "no data records can be displayed.">

<Columns>

<Asp: boundfield datafield = "employeeid" headertext = "employeeid"

Readonly = "true" sortexpression = "employeeid"/>

<Asp: boundfield datafield = "lastname" headertext = "lastname"

Sortexpression = "lastname"/>

<Asp: boundfield datafield = "firstname" headertext = "firstname"

Sortexpression = "firstname"/>

<Asp: boundfield datafield = "Address" headertext = "Address"

Sortexpression = "address"/>

</Columns>

</ASP: gridview>

Because its HTML Declaration uses the boundfield field, it can be proved that the field data value of the common data source will be displayed in text.

In comparison, DataGrid 1.0 supports only four types by default: (1) bound column, (2) button column, (3) hyperlink column, (4) template column. Therefore, it can be seen that the gridview function is richer and easier to use. While the DataGrid named by the field name still uses column, the gridview uses field.

Note

(1) The above seven field types are not only exclusive or exclusive to the gridview control, and others such as detailsview share these seven field types.

(2) Do you know why "field" is used instead of "column"? This is because the seven types of fields are mainly derived from the datacontrol-field class, therefore, fields are entered at the end of the word.

*
Set the sqldatasource Data Source

Set the sqldatasource data source to the employeeid, lastname, and photo fields of the employees data table.

Imagehandler. ashx generic processing method

Add an imagehandler. ashx generic processing method to the project. The complete program code is as follows:

01 <% @ webhandler Language = "VB" class = "imagehandler" %>

02

03 imports system

04 imports system. Web

05

06 public class imagehandler: Implements ihttphandler

07 'get database connection settings

08 shared connstring as connectionstringsettings =

Webconfigurationmanager. connectionstrings ("northwindconnectionstring ")

09

10 public sub processrequest (byval context as httpcontext)

Implements ihttphandler. processrequest

11 'Context. response. contenttype = "text/plain"

12 'Context. response. Write ("Hello World ")

13 dim MS as memorystream = nothing

14 try

15' get employee code

16 dim employeeid as string =

Context. Request. querystring ("employeeid ")

17' use the getimage () method of the ReadImage class to obtain image data in SQL Server

18 'create an SQL command

19 dim strsql as string = "select photo from employees where

Employeeid = @ paramemployeeid"

20' create sqldatasource

21 dim sqldsphoto as new

Sqldatasource (connstring. connectionstring, strsql)

22 sqldsphoto. selectparameters. Add ("paramemployeeid ",

Typecode. int32, employeeid)

23' query through sqldatasource

24 dim DV as dataview = ctype
(Sqldsphoto. Select (datasourceselectarguments. Empty), dataview)

25' return the photo field data of the first row of dataview.

26 dim photoimage as byte () = ctype (DV (0) ("photo"), byte ())

27 MS = new memorystream (photoimage, 0, photoimage. length)

28 catch ex as exception

29

30 end try

31

32 If (MS isnot nothing) then

33' get the memorystream size of the image

34 dim buffersize as integer = ctype (Ms. length, integer)

35' create a buffer

36 dim buffer as byte () = new byte (buffersize ){}

37 'call memorystream. Read, read from memorystream to buffer,

38 'and return count

39 dim countsize as integer = Ms. Read (buffer, 0, buffersize)

40' return Image Buffer

41 context. response. outputstream. Write (buffer, 0, countsize)

42 end if

43 end sub

44

45 public readonly property isreusable () as Boolean

Implements ihttphandler. isreusable

46 get

47 return false

48 end get

49 end property

50

51 end class

Program description:

The above programs read from the connection string, to the sqldatasource dynamic program construction, and the return of the dataview data type are all authentic ASP. NET 2.0 pure syntax. However, you can also use the traditional ADO. Net syntax. The priest uses the sqldata source syntax to demonstrate how to use the new generation sqldatasource syntax. The program description has been clearly stated in the annotations. It doesn't matter if you can understand it. You can copy and reference it directly in your projects.

Create templatefield

Click Edit column in The gridview Smart tag to enter the editing mode. Add a templatefield template field to accommodate the HTML Control and name headertext "employee photo ". Therefore, there are currently two boundfield fields of the employee ID and lastname, and one templatefield template field.

Use HTML to read image handler. ashx generic processing method Images

However, imagehandler. although ashx is strong, it can be used only when someone is active, and the webpage must call imagehandler through HTML . ashx, while imagehandler. after receiving the employee ID, ashx reads the binary image of the database using this employee ID. After some processing, it is output to the outputstream data stream for display by . Therefore, we need to add an HTML Control in the templatefield. Click Edit template in the gridview and select column [2]-itemtemplate project of Employee photos, drag an HTML image control from the toolbox to itemtemplate (see Figure 9-41), and set the src attribute of the image to the following key programs:

Imagehandler. ashx? Employeeid = <% # eval ("employeeid") %>

Figure 9-41 create an image control in the template

Run the program, as shown in Figure 9-42.

Figure 9-42 the running interface of the database image is dynamically displayed in the gridview

Note

(1) In the employees data table, the photo field originally had a 78 bytes shift, but because this part may cause troubles in some places, the priest reset the photo data, it starts from 0 bytes without displacement. We recommend that you use the northwind. MDF database attached to the CD for convenience.

(2) Note that the control is HTML. Do not misuse the image control of ASP. NET.

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.