Spider Web Woman Designs

Working with Access Databases in Microsoft Expression Web

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">
<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>
 

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.

Code found at Dev::Blog and tweaked a bit.

Back to Working with Access Databases in Microsoft Expression Web