Spider Web Woman Designs
In Expression Web 4, it’s a fairly simple process to display data from a database on a web page in a gridview (table). However, without formatting the fields, it’s difficult to distinguish one data field from another, and impossible to click on a link or email address. This article shows you how to format number fields (currency and percentages) and telephone numbers, and make URLs and email addresses clickable in an ASP.Net Gridview using Expression Web 4. Figure 1 shows you a gridview with no fields formatted.
Figure 1: An ASP.NET Gridview with unformatted fields
This article assumes that you have created a website, imported an Access database into it, configured your data source using the AccessDataSource Control, created a Gridview on a web page, and saved the page with an .aspx file extension.
The instructions in this tutorial were created using:
In Design View, click in the GridView and click on the arrow pointing to the right. You should see the GridView Tasks pop-up (see Figure 2).
Figure 2: Gridview Tasks pop-up
Then, follow these steps:
Figure 3: Add Field dialog
Figure 4: Choose HyperLinkField and more options will appear
For Hyperlink Text, if you want generic text to appear, type it into the Specify Text field. If you want the URL to display, choose Get Text from Data Field and select your fieldname from the dropdown. You can also customize the text using another field in your database and formatting it. The example in Figure 4 is using the firstname field as part of the Hyperlink Text.
For Hyperlink URL, choose Get URL from Data Field and select your field from the dropdown.
If the URL in your database in formatted as www.yourwebsite.com (without a preceding http://), type http://{0} in the URL format string box (see Figure 5).
Figure 5: Use http://{0} if the URL field in your database does not include http://.
In Design View, click on the column and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Then:
Figure 6: Your ASP.NET Gridview as it will appear in a browser. Click the Web Site link and a new browser window will open.
Notice that the Website field from your database is displaying twice? In Design View, click on the Gridview and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Choose Edit Columns, find the original Website field in the Selected Fields list, and click the red ‘X’ to delete it.
In Design View, click on the Gridview and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Then:
For Hyperlink text, choose Get text from data field and select your email address fieldname from the dropdown.
For Hyperlink URL, choose Get URL from data field and select your field from the dropdown.
In the URL format string field, type mailto:{0}.
You can delete the extra non-clickable email field by following the instructions at the end of the previous section.
In Figure 1, we saw fields identified as telephone numbers, salaries, etc. that really didn’t look all that clear. In this section, you learn how to format those fields to make them more usable for your site visitors.
In Design View, click on the Gridview and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Then:
This is what the code looks like:
<asp:boundfield HtmlEncode="False" DataFormatString="{0:C2}" DataField="salary" SortExpression="salary" HeaderText="salary"> </asp:boundfield>
The field in the Access database was defined as:
In Design View, click on the Gridview and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Then:
This is what the code looks like:
<asp:boundfield HtmlEncode="False" DataFormatString="{0:P2}" DataField="amount" SortExpression="amount" HeaderText="amount"> </asp:boundfield>
This option can work if your telephone number is stored in a Text field in your Access Database. In this example, my field name is "homephone".
In Design View, click on the Gridview and click on the arrow pointing to the right to bring up the GridView Tasks pop-up. Then:
<asp:templatefield SortExpression="homephone" HeaderText="homephone"> <EditItemTemplate> <asp:TextBox runat="server" Text='<%# Bind("homephone") %>' id="TextBox1"> </asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("homephone") %>' id="Label1"> </asp:Label> </ItemTemplate> </asp:templatefield>
<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval (Container.DataItem, "homephone")))%>
If you follow the instructions in this article, your web page will go from what we saw in Figure 1 to what we see now in Figure 7. I’m sure you (and your users) will agree that this is a definite improvement.
Figure 7: Your ASP.NET Gridview with formatted URL, email, and number fields.
This article was orginally written for Que Publishing and published as Formatting Fields in an ASP.NET Gridview with Microsoft Expression Web 4 .