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.
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>
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
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.
In an aspx page I am binding the labels like this:
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("date_of_joining") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# Eval("paid_priviledge_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
And in the code behind I'm binding the grid view like this :(minimum code is given)
GridView1.DataSource = dt2;
GridView1.DataBind();
But the gridview columns show the date like this :
4/12/2011 12:00:00 AM
4/4/2011 12:00:00 AM
Please suggest how to remove the time stamp part and to display only the date part.
I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.
You can specify format strings for the eval statement:
Eval("date_of_joining", "{0:dd/MM/yyyy}")
Create a FormatDate method in your codebehind, and call that from your gridview.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
http://www.csharp-examples.net/string-format-datetime/
This part will go in your code behind
private object FormatDate(DateTime input)
{
return String.Format("{0:MM/dd/yy}", input);
}
And this bit will go in your markup
<asp:TemplateField HeaderText="Date of Joining">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Priviledge Date">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.
Use "{0:d}" for short date format.
Try
Text='<%# Eval("paid_priviledge_date","{0:d}") %>'
and
Text='<%# Eval("date_of_joining", "{0:d}") %>'
You can use the DataFormatString in a bound field the same can be set as below:
<asp:Label ID="Label8" runat="server" Text='<%# Eval("paid_priviledge_date","{0:d}") %>'/>
Text='<%# (Convert.ToDateTime((Eval("date_of_joining")))).ToShortDateString() %>'
This is the simplest way I discovered.
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>