views:

30

answers:

2

i would like to display phone number fields on a website as (123) 456-7890 but have the system see it as 1234657890 when calling myTextBox.Text i am thinking this would require overriding the text property and cleaning it up in the overridden property but wanted to run this past the community.

+1  A: 

Yes, the Text property is two ways. You would indeed have to format on output, and clean it up on input. However! Let me suggest that instead of using a TextBox for output that you use a Label or LiteralControl to display it. Then when the user edits, change to a textbox that doesn't have the formatting. The concept is from the DataBoundControls like:

<ItemTemplate>
  <asp:Label Text=<%# FormattedText %> />
</ItemTemplate>
<EditItemTemplate>
  <asp:TextBox />
</EditItemTemplate>
matt-dot-net
i like that approach.
Christopher Kelly
A: 

If you are storing and retrieving the phone number as a ten-digit string, you can just format it when you display it, like this:

string.Format("{0,0:(###) ###-####}", phoneNumber);

Before you do that, you should test that the string actually contains 10 numeric characters.

DOK