Sei sulla pagina 1di 16

Lp trnh Web

Chng: 17

Data Controls
1 Lp trnh Web

Ni dung
The GridView

Formatting the GridView


Selecting a GridView Row Editing with the GridView Sorting and Paging the GridView Using GridView Templates The DetailsView and FormView

Lp trnh Web

The GridView
Automatically Generating Columns

Defining Columns

Lp trnh Web

The GridView
Automatically Generating Columns

Defining Columns

Lp trnh Web

Defining Columns: Column Types


Class BoundField ButtonField CheckBoxField CommandField HyperLinkField ImageField Description This column displays text from a field in the data source This column displays a button in this grid column This column displays a check box in this grid column This column provides selection or editing buttons This column displays its contents (a field from the data source or static text) as a hyperlink This column displays image data from a binary field (providing it can be successfully interpreted as a supported image format) This column allows you to specify multiple fields, custom controls, and arbitrary HTML using a custom template. It gives you the highest degree of control but requires the most work
Lp trnh Web

TemplateField

Defining Columns:

Lp trnh Web

Defining Columns: BoundField Properties


Property DataField DataFormatString ApplyFormatInEditMode Description Identifies the field (by name) that you want to display in this Column Formats the field. This is useful for getting the right representation of numbers and dates If true, the DataFormat string is used to format the value even when the value appears in a text box in edit mode. The default is false

FooterText, HeaderText, and Sets the text in the header and footer region of the HeaderImageUrl grid if this grid has a header and footer. To show an image in the header instead of text, set the HeaderImageUrl property ReadOnly InsertVisible If true, it prevents the value for this column from being changed in edit mode If true, it prevents the value for this column from being set in insert mode
Lp trnh Web

Defining Columns: BoundField Properties


Property Visible SortExpression HtmlEncode Description If false, the column wont be visible in the page Sorts your results based on one or more columns If true (the default), all text will be HTML encoded to prevent special characters from mangling the page Displays the text that will be shown for a null value If true, converts all empty strings to null values Configures the appearance for just this column, overriding the styles for the row
Lp trnh Web

NullDisplayText ConvertEmptyStringToNull ControlStyle, HeaderStyle, FooterStyle, and ItemStyle

Formatting the GridView


Formatting Fields

Using Styles
Formatting-Specific Values

Lp trnh Web

Formatting Fields
Each BoundField column provides a DataFormatString

property you can use to configure the appearance of numbers and dates using a format string.

<asp:BoundField DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:C}" />

Lp trnh Web

10

Formatting Fields
Numeric Format Strings: View Table 17-3. in the ebook

at page 584. Time and Date Format Strings: View Table 17-4 in the ebook at page 585

Lp trnh Web

11

Selecting a GridView Row


Adding a Select Button

Using Selection to Create Master-Details Pages

12

Lp trnh Web

Adding a Select Button


Heres an example that displays a select button:

<asp:CommandField ShowSelectButton="True" ButtonType="Button" SelectText="Select" />


And heres an example that shows a small clickable icon

<asp:CommandField ShowSelectButton="True" ButtonType="Image" SelectImageUrl="select.gif" />

Lp trnh Web

13

Using a Data Field As a Select Button


<asp:ButtonField ButtonType="Button" DataTextField="ProductID" />

<asp:ButtonField CommandName="Select" ButtonType="Button" DataTextField="ProductID" />

Lp trnh Web

14

Using Selection to Create MasterDetails Pages

Lp trnh Web

15

Sorting and Paging the GridView


Sorting

Paging

16

Lp trnh Web

Potrebbero piacerti anche