问题
Refer the code below:
<asp:TemplateField HeaderText="DOB" SortExpression="dob" >
<EditItemTemplate >
<asp:TextBox ID="TextBox3" Width="60px" runat="server" Text='<%# Bind("dob", "{0:d-M-yyyy}") %>'></asp:TextBox>
<asp:CalendarExtender ID="TextBox3_CalendarExtender" runat="server" Enabled="True" Format="d-M-yyyy" TargetControlID="TextBox3">
</asp:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("dob", "{0:d-M-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I've a DateTime column named "dob" in db. I've formatted it in "d-M-yyy" while binding. Everything works perfect except when user updates his DOB directly from GridView.
Since, I've displayed the date in textbox in "d-M-yyyy" format, while db server treats it as "mm-dd-yyyy" format, then problem arises when user wants to update data.
By the way, the server raises this:
System.FormatException: String was not recognized as a valid DateTime.
Code for Update:
UpdateCommand="update family_members_info set dob=@dob where memberID=@memberID">
<UpdateParameters>
<asp:Parameter Name="memberID" />
<asp:Parameter Name="dob" Type="DateTime" />
</UpdateParameters>
Any help would be greatly appreciated.
回答1:
hi use the date format as
<asp:TextBox ID="TextBox3" Width="60px" runat="server" Text='<%# Bind("dob", "{0:dd-MMM-yyyy}") %>'></asp:TextBox>
thanks
回答2:
In my case helped to add
<globalization culture="cs-CZ" uiCulture="cs-CZ" />
tag in the web.config file.
Source: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.71).aspx
来源:https://stackoverflow.com/questions/15083114/formatting-datetime-in-asp-net-gridview-while-updating-without-using-c-sharp