Adding Hyperlink Columns to a DataGrid Web Server Control
A Hyperlink column allows you to display text as a hyperlink that users can click to navigate to another page. The text of the hyperlink can either be static — the same text for every item — or the contents of a field in the data source. Similarly, the target page for the hyperlink can be a single page for all the hyperlinks, or a URL derived from the data source.
To add a hyperlink column
1. Add a DataGrid to the page.
2. In Design view, select the DataGrid control, then click the Property Builder link at the bottom of the Properties window.
3. In the DataGrid Properties dialog box, click the Columns tab.
4. In the Available columns box, select Hyperlink Column and click the Add button
5. Set the column’s header text and footer text. If you want to use an image rather than text for the header, select an image in the Header Image box.
Note The header and footer will be displayed only if you have selected Show header and Show footer in the General tab.
6. If you want to be able to sort by the contents of the column, set the Sort expression for the column.
7. Specify the text to display as a hyperlink using one of the following methods:
- To specify static text (same text for every row in the grid), enter it into the Text box.
- To create the hyperlink from the data source, select the name of the field from the Text field list.
8. Specify the target page for the hyperlink using one of the following methods:
- To specify a static page (same target page for every item), enter it into the URL box.
- Optionally, specify a target frame or window in the Target box.
- To use a data field as the source for the target page URL, select the field name from the URL field list. In this case, you can specify a formatting expression for the hyperlink text using the URL format string box.
To pass information to the target page
You can use URL field to pass data to a target page.
- Enter a value for the URL field box specifying the data field you wish to pass and define a format for the URL in the URL format string that includes the target page. For example, to pass the value of the FirstName field in the data source to a page Default, the URL field would be set to
FirstName, and the URL format string would be set toDefault.aspx?FirstName={0}.If you provide information in the URL Field box in this way, it is passed to the target page as a query string. In the target page you can get the value passed from the hyperlink by examining the contents of the QueryString parameter. In a Web Forms page, you can do this in the Page_Load event. The following example shows how you can retrieve the value passed to the target page.
// C#
private void Page_Load(object sender, System.EventArgs e)
{
string firstName = Request.QueryString[”FirstName”];
}
