ASP.NET: Language specific format when binding - asp.net

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"/>

Related

Formatting dates in ASP.Net data binding expressions

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>

Can i parse in ASPX-markup an int to an enumeration

I read all entries from an entity Framework/DB for a web page (ASP.NET) and in a repeater on my page i set the value of a TextBox like that:
<td>
<owiw:CustomTextBox runat="server" ReadOnly="true" ID="txt_FieldType" Text='<%# DataBinder.Eval(Container.DataItem, "iFieldType") %>' />
</td>
To iFieldType exists an enumeration which lies in a static class.
Can i parse the int value using Enum.Parse?
Must be something like that, isn't it?
Text='<%# Enum.Parse(typeof(OneWhoIsWhoModel.Database.DatabaseHelper.CustomFieldTypes), (Container.DataItem, "iFieldType")) %>'
Got it!
I have forgotten to get the value with DataBinder.Eval and then it must be converted to the type string.
The solution is always simple, just the way to it is troublesome.
<owiw:CustomTextBox runat="server" ReadOnly="true" ID="txt_FieldType"
Text='<%# Enum.Parse(typeof(OneWhoIsWhoModel.Database.DatabaseHelper.CustomFieldTypes),
Convert.ToString(DataBinder.Eval(Container.DataItem, "iFieldType"))) %>' />

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.

Format integer to date in eval function in RadGrid Template

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

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.

Resources