In a DetailsView one of the controls is a TextBox called PrimaryPhone.
PrimaryPhone is formatted using string.Format in an ASP:literal control.
The data is stored like this in the database:
9781231234
It is using a Iif function to handle the formatting of 10 digit entries.
<asp:TemplateField HeaderText="Primary Phone:" SortExpression="PrimaryPhone">
<EditItemTemplate>
<asp:TextBox ID="TextBoxPrimaryPhoneEdit" runat="server" Text='<%# Bind("PrimaryPhone") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBoxPrimaryPhoneInsert" runat="server" Text='<%# Bind("PrimaryPhone") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Literal ID="PrimaryPhoneLiteral" runat="server"
Text='<%# iif(Len(Eval("PrimaryPhone"))=10,
string.Format("{0:(###) ###-####}", Int64.Parse(Eval("PrimaryPhone").ToString())),
Eval("PrimaryPhone")) %>' />
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
Instead of the Iif function we would like to format the PrimaryPhone from a VB.Net code-behind file because the phone numbers could be:
10 digits
7 digits
Some of the phone numbers are also missing.
We would like to format it based on the length of the numbers entered into the PrimaryPhone DetailsView TextBox. We are looking to use a Case statement for this.
Is it also possible to validate the entry of the phone number in the EditItemTemplate and the InsertItemTemplate and have an Ajax validation callout extender be displayed when the phone number is not 10 or 7 digits?
Can you show the coding needed to format the PrimaryPhone from the code-behind file?
Write a public static function in the CodeBehind file and call it from the aspx page, passing the phone value.
(I use C# code here, but you should get the idea)
public static string FormatPhoneNumber(string phoneNumber)
{
// do your conditional formatting here
return result;
}
and in your ItemTemplate:
<ItemTemplate>
<asp:Literal ID="PrimaryPhoneLiteral" runat="server"
Text='<%# FormatPhoneNumber(Eval("PrimaryPhone").ToString()) %>' />
</ItemTemplate>