This is my item template
<asp:Label ID="lblDF" runat="server" Text='<%# Eval("DateLong","{0:d}")%>'></asp:Label>
How can I convert DateLong(which returns int) to Date format(MM/dd/yyyy)? I know there is a workaround in codebehind. But is it possible by just using other functions inside the code block?
thanks in advance
You can try like this...
<asp:Label ID="lblDF" runat="server" Text='<%#Convert.ToDateTime(
Eval("DateLong")).ToString("MM/dd/yyyy") %>'></asp:Label>
Edit As DateLong Is In integer..
<asp:Label ID="lblDF" runat="server" Text='<%#
DateTime.ParseExact(Eval("DateLong").ToString(), "yyyyMMdd",//Specify the format in which date stored in database
System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy");
%>'></asp:Label>
Edit2 DateLong is in formate of difference of days from given default date of db..
<asp:Label ID="lblDF" runat="server" Text='<%#
Convert.ToDateTime("1890-01-01").AddDays(3652+Convert.ToInt32(Eval("DateLong"))).ToString("MM/dd/yyyy")
%>'></asp:Label>
You just need to change the d in the format: '<%# Eval("DateLong","{0:MM/dd/yyyy}")%>'
I tried with
Text='<%#Convert.ToDateTime(Eval("DateLong")).ToString("MM/dd/yyyy") %>'
It was working fine but when it has a null value it throws an error
Related
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>
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"/>
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.
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}") %>'
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>