ASP.NET Data Web Controls
Kick Start

Resources

Newsletter

About

FAQs
 
ASP.NET Data Web Controls Kick Start
Buy the Book!

5 Stars at Amazon.com!
5-Star Rating at Amazon.com!
Amazon.com ratings updated weekly...

Join WebHost4Life.com

Advertisements
Creating a HyperLink with Multiple Querystring Values
By: Scott Mitchell | Created: 2003-05-11 | Last Updated: 2003-06-05 | Printer-Friendly Version

To follow along with the book, refer to: Chapter 4, Adding Buttons and Hyperlinks to the DataGrid Web Control



As discussed in Chapter 4 of ASP.NET Data Web Controls Kick Start, the DataGrid Web control can have a number of different column types. The most common column type is the BoundColumn, which simply displays the value of a particular DataSource field. Another useful DataGrid column type is the HyperLinkColumn, which displays a hyperlink in a DataGrid column.

When using the HyperLinkColumn, you can specify a single DataSource value for both the text and URL portions of the hyperlink. For example, imagine you worked for a company that manufactured widgets. Designing an information Internet site for your company, you decide to have a DataGrid used to list the various widgets. Along with each widget listed, you want to provide a link that, if clicked, takes the user to a page that displays detailed information about the particular widget.

If the information about your widgets is stored in a database table with a single primary key that uniquely identifies each widget (like WidgetID), this can be easily accomplished by using a HyperLinkColumn and having a hyperlink direct the user to some details.aspx page, passing along the WidgetID of the widget the user wants to learn more about. A sample DataGrid that would accomplish this can be seen below:

<asp:DataGrid runat="server" id="dgWidgets" AutoGenerateColumns="False" ...> <Columns> ... <asp:HyperLinkColumn Text="View Details" DataNavigateUrlField="WidgetID" DataNavigateUrlFormatString="details.aspx?WidgetID={0}" /> ... </Columns> </asp:DataGrid>

Pretty straightforward. However, what if you needed to pass two (or more) databound parameters through the querystring? The HyperLinkColumn is not designed to accomodate such a need; instead we must use a TemplateColumn that contains in its ItemTemplate a HyperLink Web control. In this HyperLink Web control we can use databinding syntax to populate the NavigateUrl property with appropriate querystring values.


Passing Multiple Parameters Through the Querystring Using a TemplateColumn
Imagine that in our previous example we wanted to pass both the widget's WidgetID and Price field values through the querystring to the details.aspx page. In order to accomplish this with a TemplateColumn, we'd use the following in place of the HyperLinkColumn:

<asp:DataGrid runat="server" id="dgWidgets" AutoGenerateColumns="False" ...> <Columns> ... <asp:TemplateColumn> <ItemTemplate> <asp:HyperLink runat="server" Text="View Details" NavigateUrl='<%# "details.aspx?WidgetID=" & _ Container.DataItem("WidgetID") & _ "&Price=" & Container.DataItem("Price") %>' /> </ItemTemplate> </asp:TemplateColumn> ... </Columns> </asp:DataGrid>
VB.NET

 

That's all there is to it! Realize that the technique illustrated here can also be used to allow for two or more databound fields in the text portion of the hyperlink.

<shameless plug>
Chapter 4, Adding Buttons and Hyperlinks to the DataGrid Web Control, contains a thorough examination of the HyperLinkColumn.
</shameless plug>


Home | FAQs | Articles | About | My Blog | Buy the Book!

Copyright 2006, Scott Mitchell. All Rights Reserved.