Formatting dates in ASP.Net data binding expressions - asp.net

Can you format a date when using a data binding expression in ASP.NET?
DataFormatString works for a BoundField in a GridView, but not in a ItemTemplate label using a data binding expression. Ideally I'd like to use a custom format string like {0:dd-MMM-yyyy} to format the date.
<ItemTemplate>
<asp:Label ID="lblLabel" runat="server" Text='<%# Bind("FIELD_NAME")%>'></asp:Label>
</ItemTemplate>

you could use
<ItemTemplate>
<asp:Label ID="lblLabel" runat="server"
Text='<%# Eval("FIELD_NAME", "{0:dd-MMM-yyyy}") %>'>
</asp:Label>
</ItemTemplate>

Related

ASP.NET: Language specific format when binding

I have a date TextBox that is bound to a DateTime property:
<asp:TemplateField HeaderText="Date1">
<ItemTemplate>
<asp:TextBox ID="Date1TextBox" Text='<%# Bind("Date1", "{0: yyyy-MM-dd}")%>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
What is the easiest way to make the format dynamic, meaning that I want to be able to specify which format I want to use dependent on the current ui culture / language. Is it possible to do this in markup (I want to avoid to do this in Code if possible)?
I tried the following but Bind doesn't seem to support methods to get the format string:
<asp:TextBox ID="Date1TextBox" Text='<%# Bind("Date1", GetCurrentFormat())%>' runat="server"/>
try this:
<asp:TextBox ID="Date1TextBox" Text='<%# Eval("Date1","{0:yyyy-MM-dd}") %>'
runat="server"/>
EDIT
protected string GetCurrentFormat(string myDate)
{
//Retrive Current Format from DB
string MyFormat="yyyy-MM-dd";
return Convert.ToDateTime(myDate).ToString(MyFormat);
}
<asp:TextBox ID="Date1TextBox"
Text='<%# GetCurrentFormat(Convert.ToString(Eval("Date1"))) %>'
runat="server"/>

Calendar Return

I'm using a Calendar object on a GridView. Currently it works but when I want to update my row I can't choose the return of my object.
On update it return to me : MMM JJ YYYY HH:MM AM/PM -> avr 13 2014 12:00AM
I just want a JJ/MM/YYYY format !
<EditItemTemplate>
<asp:Calendar ID="Cal_date_debut" runat="server" SkinID="Calendar" SelectedDate='<%# Bind("date_debut_session") %>'></asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("date_debut_session") %>'></asp:Label>
</ItemTemplate>
There is the code to display my calendar.
Anyone see how to do that ?
It was a CultureInfo problem
try this
<EditItemTemplate>
<asp:Calendar ID="Cal_date_debut" runat="server" SkinID="Calendar" SelectedDate='<%# Bind("date_debut_session") %>' VisibleDate='<%# Bind("date_debut_session") %>' >
</asp:Calendar>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("date_debut_session", "{0:d}") %>'></asp:Label>
</ItemTemplate>
as you can see the SelectedDate is passe from the db (I guess) as a DateTime object, instead the {0:d} format string transform the DateTime from db into a string formatted like dd/MM/yyyy, but there are other options to do that.
From the codebehind you could get the same output by formatting the Cal_date_debut.SelectedDate like this
Cal_date_debut.SelectedDate.ToString("dd/MM/yyyy");
Notice the VisibleDate property, binded to date_debut_session.
I've tried to update the columns using a sqlDataSource and it worked.

How do I format numerical data as thousands in a GridView Column ItemTemplate?

I have a gridview with columns like so:
<asp:TemplateField HeaderStyle-Width="75px">
<HeaderTemplate>
<asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:#0}")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
The numbers in this column are often greater than 1000, so I'd like to format them as such. For example, if the data in this column reads 11359, I'd like it to format the number as 11,359.
I have attempted the following:
<asp:TemplateField HeaderStyle-Width="75px">
<HeaderTemplate>
<asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:N0}")%>' runat="server">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
But the above generates an exception: Input string was not in a correct format
What am I doing wrong?
You could do:
<asp:Label ID="lblM1" Text='<%# Eval("m1","{0:0,0}")%>' runat="server"></asp:Label>
That should format 11239 as "11.239". The Group Separator would be different depending on your culture.
Take a look to the documentation:
The , custom specifier
and
Standard Numeric Format Strings
Custom Numeric Format Strings
EDIT:
By the way, it could be a completely different reason. It could be that you're sending the data in one culture, but .Net it's trying to parse with a different one that's not compatible.
I tried your format and it worked for me, thanks :)
just change as follow and try if it works for u.
'>
instead of Eval use DataBinder.EVal.
simply use this-
Text='<%# Eval("m1","{0:0,0}") %>'

Format a date when reading into GridView

I am reading a Date from a Table into a GridView, and it appears formatted in the GridView as:
15/01/2012 00:00:00
How can I read it in so that just the date part appears, minus the 00:00:00?
And when I use UPDATE, how do I store it back to the Table?
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SDate") %>' >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("SDate")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
If you're using a BoundField to display the date, it has a property called DataFormatString you can set that will format your field to the simple date format you'd like:
<asp:BoundField ... DataFormatString="{0:dd/MM/yyyy}" ... runat="server" />
If you'd doing something with a TemplateField, you can specify the date format in the binding expression:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ...>
<%# Eval(Container.DataItem, "DateProperty", "{0:dd/MM/yyyy}") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Because you're only changing how the data is displayed, the full underlying date is still available to you when you perform your SQL UPDATE, assuming you're retrieving your data via the DataItem property of each GridViewRow.

Databinding Error when Recreating object

Figure there is a simple solution to this problem but I have been unable to find it.
I have databinding in an ASP.Net application to a GridView. This gridview is bound to an ObjectDataSource as per standard usage.
The problem I have is that one of my bound fields uses the property DataFormatString="{0:C}" and due to the currency format being displayed when an update is attempted and the object recreated I get a error as such "$13.00 is not a valid value for Decimal."
Clearly this is a result of the column using a FormatString and then attempting to bind it back to a decimal property I have in my object called UnitPrice.
I am assuming there is some markup I can set that can specify how the value is translated back?
Thanks in advance for any help.
For anyone curious the solution ended up looking like this...
<asp:TemplateField>
<HeaderTemplate>
UnitPrice
</HeaderTemplate>
<EditItemTemplate>
<asp:Label ID="lblEditItem" runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' Enabled="false" ></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="lblUnitPrice"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Do not include the format string in the EditItemTemplate. Just bind the raw value.
Something like this:
<asp:TemplateField SortExpression="UnitPrice" HeaderText="Unit Price">
<EditItemTemplate>
<asp:TextBox ID="editUnitPrice" Runat="server" Text='<%# Bind("UnitPrice", "{0:#,##0.00}") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>' ID="Label1"> </asp:Label>
</ItemTemplate>
</asp:TemplateField>

Resources