Online Book Reader

Home Category

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

By Root 5242 0
of the BoundField class doesn’t work properly without the additional attribute HtmlEncode=“false”. The reason is because ASP.NET first HTML-encodes the value of bound field and then applies the formatting. But at that point, the bound value is no longer affected by the specified format string. Enabling HTML-encoding earlier in the cycle is a security measure aimed at preventing cross-site scripting attacks.

Hyperlink Fields


Hyperlink columns point the user to a different URL, optionally displayed in an inner frame. Both the text and URL of the link can be obtained from the bound source. In particular, the URL can be set in either of two ways: through a direct binding to a data source field or by using a hard-coded URL with a customized query string. You choose the direct binding if the URL is stored in one of the data source fields. In this case, you set the DataNavigateUrlFields property to the name of the column. In some situations, though, the URL to access is application specific and is not stored in the data source. In this case, you can set the DataNavigateUrlFormatString property with a hard-coded URL and with an array of parameters in the query string, as follows:

HeaderText="Product"

DataNavigateUrlFields="productid"

DataNavigateUrlFormatString="productinfo.aspx?id={0}"

Target="ProductView" />

When the user clicks, the browser fills the specified frame window with the contents of the productinfo.aspx?id=xxx URL, where xxx comes from the productid field. The URL can include multiple parameters. To include more data-bound values, just set the DataNavigateUrlFields property to a comma-separated list of field names. This behavior extends that of the DataGrid’s hyperlink column in that it supports multiple parameters.

The text of the hyperlink can be formatted too. The DataTextFormatString property can contain any valid markup and uses the {0} placeholder to reserve space for the data-bound value. (See Figure 10-7.)

Figure 10-7. Hyperlink fields in a GridView control.

Check Box Fields


The CheckBoxField column is a relatively simple bound column that displays a check box. You can bind it only to a data field that contains Boolean values. A valid Boolean value is a value taken from a column of type Bit in a SQL Server table (and analogous types in other databases) or a property of type bool if the control is bound to a custom collection. Any other form of binding will result in a parsing exception. In particular, you get an exception if you bind a CheckBoxField column to an integer property, thus implicitly assuming that 0 is false and a nonzero value is true.

Image Fields


The ImageField column type represents a field that is displayed as an image in a data-bound control. The cell contains an element, so the underlying field must reference a valid URL. You can compose the URL at will, though. For example, you can use the DataImageUrlField to perform a direct binding where the content of the field fills the Src attribute of the tag. Alternatively, you can make the column cells point to an external page (or HTTP handler) that retrieves the bytes of the image from any source and passes them down to the browser. The following code illustrates this approach:

DataImageUrlFormatString="showemployeepicture.ashx?id={0}"

DataAlternateTextField="lastname">

<%# Eval("titleofcourtesy") + " " +

Eval("lastname") + ", " +

Eval("firstname") %>

<%# Eval("title")%>


<%# Eval("notes")%>

Cells in the ImageField column are filled with the output of the next URL:

ShowEmployeePicture.ashx?id=xxx

Obviously, xxx is the value in the employeeid field associated with DataImageUrlField. Interestingly enough, the alternate text can also be data bound. To do this, you use

Return Main Page Previous Page Next Page

®Online Book Reader