JavaScript dateFormat error? - asp.net

I am trying to get a field named BASVURUTARIHI(Date) from DataSet. But if I only bind it with Eval, it shows dd/mm/yyyy HH/MM/SS. I don't want the hours and minutes. So, I am trying to change the dateformat but it gives me the error:
CS0103: The name 'dateFormat' does not exist in the current context
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# dateFormat(Eval("BASVURUTARIHI"),"dd/mm/yyyy") %>'></dx:ASPxLabel>
I don't know a lot about JavaScript. Can you help me with this please?

Try this:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BASVURUTARIHI", "{0:dd MMM yyyy}")%>'></dx:ASPxLabel>

This is ASP.NET code, try to have this instead:
Text='<%# ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>
Edit: to avoid error when the value is null, change to:
Text='<%# (Eval("BASVURUTARIHI") == null) ? "" : ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>

You can use this code:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# date("j/ n/ Y",Eval("BASVURUTARIHI")) %>'>
</dx:ASPxLabel>

Related

Use of Resources in Eval syntax

I'm wondering how I can use the Resources within the below Label - I'm not entirely sure.
The original line with a replacement "Yes" or "No":
<asp:Label runat="server" ID="someId" Text='<%# (Boolean.Parse(Eval("BooleanValue").ToString())) ? "Yes" : "No" %>'></asp:Label>
What I believed I could do with the resources:
<asp:Label runat="server" ID="someId" Text='<%# (Boolean.Parse(Eval("BooleanValue").ToString())) ? '<%$ Resources:language, Yes%>' : '<%$ Resources:language, No%>' %>'></asp:Label>
Any help would be appreciated, Thanks.
Okay, I got around this by declaring the variables in the code behind
protected string yes;
protected string no;
Then updating them once my page had loaded based on the culture the user set.
yes = language.ResourceManager.GetString("Yes",CultureInfo.CurrentCulture);
no = language.ResourceManager.GetString("No", CultureInfo.CurrentCulture);
Then using those in the statement
<asp:Label runat="server" ID="someId" Text='<%# (bool)Eval("BooleanValue") == false ? no.ToString() : yes.ToString() %>'></asp:Label>

suppress unit display in label

I usually use this syntax to display the unit symbol in a label: ej. 5'230 €
<asp:Label runat="server" Text='<%# Eval("TotalAmount","{0:#,###.##} €") %>' id="LabelTotal"/>
The ugly thing about is that the symbol is displayed even if the value is null or empty. Then I would like the symbol just to be suppressed.
Anyone has an idea how to do that?
Martin
You can use the following snippet
<asp:Label runat="server" Text='<%# Convert.ToDecimal(Eval("TotalAmount")) > 0 ? string.Format("{0:C}", Convert.ToDecimal(Eval("TotalAmount"))) : string.Empty %>' id="LabelTotal"/>
If the value that is to be evaluated can contain null values, you need to check for IsNullOrEmpty first before conversion.
<asp:Label runat="server" Text='<%# !string.IsNullOrEmpty(Eval("TotalAmount").ToString()) ? Convert.ToDecimal(Eval("TotalAmount")) > 0 ? string.Format("{0:C}", Convert.ToDecimal(Eval("TotalAmount"))) : string.Empty : string.Empty %>' id="Label1"/>

getting error expression expected in asp.net, when trying to check radiobutton using db value

There are 4 readiobutton inside the repeater and i'm trying to show the checked radiobutton from database value.
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IIF(Eval("ANSWER")==1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
Second approach
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# Eval("ANSWER")==1 ? true : false %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
and so on for the rest radiobutton. But, it showing the error Expression Expected error. Need help. !!
It looks like you've got your C# and VB.Net mixed. Your first example looks like VB, the second like C#. However, you've got a few problems in your VB implementation:
The equality operator in VB is =, not ==
You should use the IF operator, not the IIF function, which is obsolete
The correct code should be as follows:
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IF(Eval("ANSWER")=1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />

how to apply particular FORMAT of date by using eval?

I have used following code :
<asp:HyperLink ID="Time" runat="server" Text='<%#Eval("CREATED_ON")%>'> </asp:HyperLink>
It will display the date in the format: 11/4/2010 10:52:33 AM
But I want it to display 11/4/2010. How would I do this?
You should be able to use something like this:
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#Eval("CREATED_ON", "{0:dd/M/yyyy}")%>'> </asp:HyperLink>
You can try this -
<asp:HyperLink ID="lnkCreatedDate1" runat="server" Text='<%# DateTime.Parse(Eval("CREATED_ON").ToString()).ToString("d") %>'> </asp:HyperLink>
this should do the trick:
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CREATED_ON", "{0:dd/MM/yyyy}") %>'></asp:HyperLink>
I suppose you have a DateTime variable named MyDate:
DateTime MyDate;
If you want juste the date part:
MyDate.Value.ToString("d");
With the day:
MyDate.Value.ToString("D");
Here is a usefull doc PDF Doc
I Was Face Problem When Date Is NULL.
It Will Help Me When CREATED_ON Date Is Null.
Use This Code When You Are Not Sure That Your Date Is NULL OR NOT.
<asp:HyperLink ID="lnkCreatedDate1" runat="server" Text='<%# (String.IsNullOrEmpty(Eval("CREATED_ON").ToString())) ? "" : DateTime.Parse(Eval("CREATED_ON").ToString()).ToString("d") %>'></asp:HyperLink>
You Can try THis Also
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#Eval("CREATED_ON", "{0:d}")%>'> </asp:HyperLink>

Formatting DataBinder.Eval data

How can I format data coming from a DataBinder.Eval statement in an ASPX page?
For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.
The code for this goes like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
<tr><td >
<a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
<asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
</a>
</td></tr>
<tr><td>
<asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
</td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>
Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error?
If this is not possible, is there a way to do inline formatting?
There is an optional overload for DataBinder.Eval to supply formatting:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:
<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>
After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.
The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.
So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:
default.aspx
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
default.aspx.cs code:
public partial class _Default : System.Web.UI.Page
{
protected string GetDateInHomepageFormat(DateTime d)
{
string retValue = "";
// Do all processing required and return value
return retValue;
}
}
Hope this helps others as well.
Why not use the simpler syntax?
<asp:Label id="lblNewsDate" runat="server" Text='<%# Eval("publishedDate", "{0:dddd d MMMM}") %>'</label>
This is the template control "Eval" that takes in the expression and the string format:
protected internal string Eval(
string expression,
string format
)
http://msdn.microsoft.com/en-us/library/3d2sz789.aspx
You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.
You also can format your field inline:
<%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
If you use ASP.NET 2.0 or newer you can write this as below:
<%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
Another option is to bind the value to label at OnItemDataBound event.
This line solved my problem:
<%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
To format the date using the local date format use:
<%#((DateTime)Eval("ExpDate")).ToString("d")%>
How to Format an Eval Statement to Display a Date using Date Locale
Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.
Mark Up:-
<asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
Code behind:-
Public Function fLabel(ByVal tval) As String
fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
End Function
Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'
This works for the format as you want
<asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
You can use it this way in aspx page
<%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>

Resources