We are using string.Format(“{0:(###) ###-####}” to display formatted phone numbers in a DetailsView. An example of the phone number is:
(516) 123-4567
<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>
It formats nicely when the phone number is exactly 10 numbers and it allows the user to edit the phone. The problem we are getting is when the user erases the phone number and tries to click the Update button. The data is not saved until the user types something into the TextBox even though the database table column does allow nulls.
Did I make a mistake in my coding? We would like to set it up in such a way in case the user clears out the phone TextBox prior to clicking the Update button.
One mistake I’ve made in the past is, if a textbox is null, you need to set the corresponding variable to null as well.
For example, my code looked something like:
but in the case that the control IS empty, you need to set the corresponding variable to null:
Edit:
To be honest I really only work with code behind stuff, so not too familiar with the aspx side (like the template fields and all that). But anyway, assuming you can reach the content of your desired control (I’ll just call it PhoneNumberTextBox), and assuming you have a button named UpdateButton: