1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 56 pps

10 215 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 610,55 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

NullDisplayText—Enables you to specify text displayed when a data item has the value Nothing null... You can use a CommandField to customize the appearance of the Edit, Delete, Update, C

Trang 1

LISTING 11.18 ShowBoundField.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show BoundField</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:GridView

id=”grdMovies”

DataSourceID=”srcMovies”

AutoGenerateColumns=”false”

Runat=”server”>

<Columns>

<asp:BoundField

DataField=”Title”

HeaderText=”Movie Title” />

<asp:BoundField

DataField=”Director”

HeaderText=”Movie Director” />

<asp:BoundField

DataField=”BoxOfficeTotals”

DataFormatString=”{0:c}”

HtmlEncode=”false”

HeaderText=”Box Office Totals” />

</Columns>

</asp:GridView>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT * FROM Movies”

Runat=”server” />

</div>

</form>

</body>

</html>

Trang 2

The GridView control includes an AutoGenerateColumns property assigned the value

False If you don’t disable automatically generated columns, both columns represented by

the BoundFields and all the columns from the data source display redundantly

In Listing 11.18, BoundFields display the Title, Director, and BoxOfficeTotals columns

The DataField property represents the column that a BoundField displays The

HeaderText property determines the column header

The BoundField used to display the BoxOfficeTotals column includes a DataFormatString

property This format string formats the values of the BoxOfficeTotals column as a

currency amount

NOTE

For more information about string formatting, see the Formatting Types topic in the

Microsoft NET Framework Documentation

A BoundField supports several other useful properties:

AccessibleHeaderText—Enables you to add an HTML abbr attribute to the column

header

ApplyFormatInEditMode—Enables you to apply the DataFormatString to the field

when the row is in edit display mode

ConvertEmptyStringToNull—Enables you to convert an empty string ”” into the

value Nothing (null) when editing a column

DataField—Enables you to specify the name of the field that the BoundField

displays

DataFormatString—Enables you to use a format string to format a data item

FooterStyle—Enables you to format the column footer

FooterText—Enables you to display text in the column footer

HeaderImageUrl—Enables you to display an image in the column header

HeaderStyle—Enables you to format the column header

HeaderText—Enables you to display text in the column header

HtmlEncode—Enables you to HTML-encode the value of a data item, which enables

you to avoid script injection attacks

InsertVisible—Enables you to not display a column when inserting a new record

(does not apply to the GridView control)

ItemStyle—Enables you to format a data item

NullDisplayText—Enables you to specify text displayed when a data item has the

value Nothing (null)

Trang 3

ReadOnly—Enables you to prevent the data item from being edited in edit mode

ShowHeader—Enables you to display the column header

SortExpression—Enables you to associate a sort expression with the column

Visible—Enables you to hide a column

A CheckBoxField, as you can probably guess, displays a check box When a row is not in

edit mode, the check box displays but is disabled

The page in Listing 11.19 illustrates how you can use a CheckBoxField (see Figure 11.14)

FIGURE 11.14 Using the CheckBoxField with the GridView control

LISTING 11.19 ShowCheckBoxField.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show CheckBoxField</title>

</head>

Trang 4

<body>

<form id=”form1” runat=”server”>

<div>

<asp:GridView

id=”grdMovies”

DataSourceID=”srcMovies”

DataKeyNames=”Id”

AutoGenerateColumns=”false”

AutoGenerateEditButton=”true”

Runat=”server”>

<Columns>

<asp:BoundField

DataField=”Title”

HeaderText=”Movie Title” />

<asp:CheckBoxField

DataField=”InTheaters”

HeaderText=”In Theaters” />

</Columns>

</asp:GridView>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Id,Title,InTheaters FROM Movies”

UpdateCommand=”UPDATE Movies SET

Title=@Title, InTheaters=@InTheaters

WHERE Id=@Id”

Runat=”server” />

</div>

</form>

</body>

</html>

The CheckBoxField inherits from the BoundField class, so it includes all the properties of

the BoundField class It also supports the following property:

Text—Displays text next to each check box

You can use a CommandField to customize the appearance of the Edit, Delete, Update,

Cancel, and Select buttons displayed by the GridView control For example, the page in

Listing 11.20 uses icons for the standard edit buttons (see Figure 11.15)

Trang 5

LISTING 11.20 ShowCommandField.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show CommandField</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:GridView

id=”grdMovies”

DataSourceID=”srcMovies”

DataKeyNames=”Id”

AutoGenerateColumns=”false”

Runat=”server”>

<Columns>

<asp:CommandField

ButtonType=”Image”

FIGURE 11.15 Using a CommandField with the GridView control

Trang 6

ShowEditButton=”true”

EditText=”Edit Movie”

EditImageUrl=”Edit.gif”

UpdateText=”Update Movie”

UpdateImageUrl=”Update.gif”

ShowCancelButton=”true”

CancelText=”Cancel Edit”

CancelImageUrl=”Cancel.gif”

ShowDeleteButton=”true”

DeleteText=”Delete Movie”

DeleteImageUrl=”Delete.gif” />

<asp:BoundField

DataField=”Title”

HeaderText=”Movie Title” />

<asp:BoundField

DataField=”Director”

HeaderText=”Movie Director” />

</Columns>

</asp:GridView>

<asp:SqlDataSource

id=”srcMovies”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Id,Title,Director FROM Movies”

UpdateCommand=”UPDATE Movies SET

Title=@Title, Director=@Director

WHERE Id=@Id”

DeleteCommand=”DELETE Movies

WHERE Id=@Id”

Runat=”server” />

</div>

</form>

</body>

</html>

You do not enable the AutoGenerateEditButton or AutoGenerateDeleteButton properties

when using a CommandField Instead, you use the CommandField to set up the standard

editing buttons explicitly

The CommandField supports the following properties:

ButtonType—Enables you to specify the type of button displayed by the

CommandField Possible values are Button, Image, and Link

CancelImageUrl—Enables you to specify an image to display for the Cancel button

Trang 7

CancelText—Enables you to specify the text to display for the Cancel button

CausesValidation—Enables you to disable validation when an Edit button is clicked

DeleteImageUrl—Enables you to specify an image to display for the Delete button

DeleteText—Enables you to specify the text to display for the Delete button

EditImageUrl—Enables you to specify an image to display for the Edit button

EditText—Enables you to specify the text to display for the Edit button

InsertImageUrl—Enables you to specify an image to display for the Insert button

InsertText—Enables you to specify the text to display for the Insert button

NewImageUrl—Enables you to specify an image to display for the New button (does

not apply to GridView)

NewText—Enables you to specify the text to display for the New button

SelectImageUrl—Enables you to specify the image to display for the Select button

SelectText—Enables you to specify the text to display for the Select button

ShowCancelButton—Enables you to display the Cancel button

ShowDeleteButton—Enables you to display the Delete button

ShowEditButton—Enables you to display the Edit button

ShowInsertButton—Enables you to display the Insert button (does not apply to

GridView)

ShowSelectButton—Enables you to display the Select button

UpdateImageUrl—Enables you to specify the image to display for the Update button

UpdateText—Enables you to specify the text to display for the Update button

ValidationGroup—Enables you to associate the edit buttons with a validation group

Using Button Fields

You use a ButtonField to display a button in a GridView You can use ButtonField to

represent a custom command or one of the standard edit commands

For example, GridView in Listing 11.21 contains two ButtonFields that a user can click to

change the display order of the movie category records (see Figure 11.16)

Trang 8

LISTING 11.21 ShowButtonField.aspx

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

protected void grdMovieCategories_RowCommand(object sender,

➥GridViewCommandEventArgs e)

{

int index = Int32.Parse((string)e.CommandArgument);

int id = (int)grdMovieCategories.DataKeys[index].Values[“Id”];

int position = (int)grdMovieCategories.DataKeys[index].Values[“Position”];

switch (e.CommandName)

{

case “Up”:

position ;

break;

case “Down”:

position++;

break;

}

srcMovieCategories.UpdateParameters[“Id”].DefaultValue = id.ToString();

FIGURE 11.16 Using ButtonFields with the GridView control

Trang 9

srcMovieCategories.UpdateParameters[“Position”].DefaultValue =

position.ToString();

srcMovieCategories.Update();

}

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<title>Show ButtonField</title>

</head>

<body>

<form id=”form1” runat=”server”>

<div>

<asp:GridView

id=”grdMovieCategories”

DataSourceID=”srcMovieCategories”

DataKeyNames=”Id,Position”

AutoGenerateColumns=”false”

OnRowCommand=”grdMovieCategories_RowCommand”

Runat=”server”>

<Columns>

<asp:ButtonField

Text=”Move Up”

CommandName=”Up” />

<asp:ButtonField

Text=”Move Down”

CommandName=”Down” />

<asp:BoundField

DataField=”Position”

HeaderText=”Position” />

<asp:BoundField

DataField=”Name”

HeaderText=”Category Name” />

</Columns>

</asp:GridView>

<asp:SqlDataSource

id=”srcMovieCategories”

ConnectionString=”<%$ ConnectionStrings:Movies %>”

SelectCommand=”SELECT Id, Name, Position FROM MovieCategories

ORDER BY Position”

UpdateCommand=”UPDATE MovieCategories SET

Position=@Position WHERE Id=@Id”

Runat=”server”>

<UpdateParameters>

Trang 10

<asp:Parameter

Name=”Id” />

<asp:Parameter

Name=”Position” />

</UpdateParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

When you click either the Move Up or Move Down buttons in the page in Listing 11.21,

the GridView control’s RowCommand event is raised This event is handled by the

grdMovieCategories_RowCommand() method

The grdMovieCategories_RowCommand() method retrieves the index of the row containing

the button that was clicked The row index is grabbed from the

GridViewCommandEventArgs’s CommandArgument property passed as the second parameter to

the event handler

The grdMovieCategories_RowCommand() method updates the position of a record by

setting the SqlDataSource control’s Update parameters and calling the SqlDataSource

control’s Update() method

A ButtonField supports the following properties:

ButtonType—Enables you to specify the type of button displayed by the

CommandField Possible values are Button, Image, and Link

CausesValidation—Enables you to disable validation when the button is clicked

CommandName—Enables you to associate a standard edit command with the

ButtonField Possible values include Delete, Edit, Update, and Cancel

DataTextField—Enables you to use a data column to specify the button text

DataTextFormatString—Enables you to format the button text

Text—Enables you to specify the button text

ValidationGroup—Enables you to associate the button with a validation group

You can use CommandName to associate a ButtonField with one of the standard edit

commands For example, you can create a Delete button by assigning the value Delete to

the CommandName property

Ngày đăng: 06/07/2014, 18:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN