开发者

TextBox: Display value in one format but return another format

开发者 https://www.devze.com 2023-01-06 11:03 出处:网络
开发者_运维问答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 overridi
开发者_运维问答

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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号