Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [235]

By Root 5420 0
controls.

ItemStyle

Gets the style object for the various columns’ cells.

ShowHeader

Indicates whether the column’s header is rendered.

SortExpression

Gets and sets the expression used to sort the grid contents when the column’s header is clicked. Typically, this string property is set to the name of the bound data field.

The properties listed in the table represent a subset of the properties that each column type actually provides. In particular, each type of column defines a tailor-made set of properties to define and configure the bound field.

Bound Fields


The BoundField class represents a field that is displayed as plain text in a data-bound control such as GridView or DetailsView. To specify the field to display, you set the DataField property to the field’s name. You can apply a custom formatting string to the displayed value by setting the DataFormatString property. The NullDisplayText property lets you specify alternative text to display should the value be null. Finally, by setting the ConvertEmptyStringToNull property to true, you force the class to consider empty strings as null values.

A BoundField can be programmatically hidden from view through the Visible property, while the ReadOnly property prevents the displayed value from being modified in edit mode. To display a caption in the header or footer sections, set the HeaderText and FooterText properties, respectively. You can also choose to display an image in the header instead of text. In this case, you set the HeaderImageUrl property.

Button Fields


A button field is useful to put a clickable element in a grid’s column. You typically use a button field to trigger an action against the current row. A button field represents any action that you want to handle through a server-side event. When the button is clicked, the page posts back and fires a RowCommand event. Figure 10-6 shows a sample.

Figure 10-6. Button fields in a GridView control.

The following listing shows the markup code behind the grid in the figure:

AutoGenerateColumns="false" AllowPaging="true"

OnRowCommand="GridView1_RowCommand">

headertext="Product" />

headertext="Packaging" />

headertext="Price"

htmlencode="false"

DataFormatString="{0:c}">

Product information is displayed using a few BoundField objects. The sample button column allows you to add the product to the shopping cart. When users click the button, the RowCommand server event is fired. In case multiple button columns are available, the CommandName attribute lets you figure out which button was clicked. The value you assign to CommandName is any unique string that the code-behind class can understand. Here’s an example:

void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName.Equals("Add"))

{

// Get the index of the clicked row

int index = Convert.ToInt32(e.CommandArgument);

// Create a new shopping item and add it to the cart

AddToShoppingCart(index);

}

}

In the sample, the button column shows fixed text for all data items. You get this by setting the Text property on the ButtonField class. If you want to bind the button text to a particular field on the current data item, you set the DataTextField property to the name of that field.

You can choose different styles for the button—push, link, or image. To render the button as an image, do as follows:

ImageUrl="/images/cart.gif" />

To add a ToolTip to the button (or the image), you need to handle the RowCreated event. (I’ll discuss this in more detail later in the chapter.)

Note

The DataFormatString property

Return Main Page Previous Page Next Page

®Online Book Reader