Sunset at Oak Island, North Carolina

Spider Web Woman Designs

How to format a Telephone Number field in an ASP.NET GridView

This assumes your telephone number is stored in a Text field in your Access Database. In this example, my field name is "homephone".

Common GridView Tasks > Edit Columns

Select the field containing the phone number from the Selected fields list.

Click the Convert this field into a TemplateField link.

Click OK.

Switch to Code View.

Find the generated code for the phone number field:

<asp:templatefield SortExpression="homephone" HeaderText="homephone">
<asp:TextBox runat="server" Text='<%# Bind("homephone") %>' id="TextBox1">
<asp:Label runat="server" Text='<%# Bind("homephone") %>' id="Label1">

In the ItemTemplate node (highlighted above in blue), select the code highlighted above in yellow and replace it with this code:

<%# String.Format("{0:(###) ###-####}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "homephone")))%>

Save your page and preview in browser.

Back to Working with Access Databases in Microsoft Expression Web


Email UsWe're on Google+LinkedIn ProfileFollow us on TwitterLike Us on Facebook!