Use of Resources in Eval syntax - asp.net

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>

Related

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

How to use Eval function to validate an integer field & then show a text value?

I have this code :
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<%#Eval("IsActive")%>
</ItemTemplate>
</asp:TemplateField>
I have to use Eval to validate IsActive field, which is of type INTEGER.
It can contain 1 or 0. By checking this value, I have to show to the user the output Yes or NO, because I don't want to show 1/0.
Can you please tell me how to do it ?
Thanks in advance ;)
Perhaps:
<%# (int)Eval("IsActive") == 1 ? "Yes" : "No" %>
<% #Eval("IsActive") == 1 ? "Yes" : "No" %>
if value is integer the lblsuccess will be shown. And if value is not integer then lblerror is shown. Place this code inside item template
<asp:Label id="lblsuccess" runat="server" Text="value is integer"
Visible='<%# Int.TryParse("IntValue") ; %>' ></asp:Label>
<asp:Label id="lblerror" runat="server" Text="value is not integer"
Visible='<%# !Int.TryParse("IntValue") ; %>' ></asp:Label>

JavaScript dateFormat error?

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>

Customize label text in grid view template field

I want to show "N/A" text in grid view label if value is not available in database and if it is available, then the value should be displayed instead of "N/A".
How can I customize my label?
This is the code that I have written to get the value.
<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# Eval("CineRunFrom") %>'></asp:Label>
This works:
<asp:Label id="dada" runat="server" Text='<%# string.Format("{0}",string.IsNullOrEmpty(Eval("CineRunFrom").ToString())?"N/A":Eval("CineRunFrom")) %>' ></asp:Label>
You may use this: Text='<%# Eval("CineRunFrom")?? "N/A" %>'
Add a new function in code behind & call it from HTML code, check sample code below.
Code
Private Function GetDisplayText(ByVal CineRunFrom As String) As String
'Do whatever you want here and return text to dispaly as required
End Function
HTML
<asp:Label ID="lblCineRunFrom" runat="server" Text='<%# GetDisplayText(Eval("CineRunFrom")) %>'></asp:Label>

populate lable control inside a gridview based on inline Conditional statement

I am trying to display a plain text in the column(contains a label) of gridview based on a condition. Here is my erroneous code. Please correct.
<asp:Label ID="lblAsgn" runat="server" Text= '<%#Eval("StatusId") == 0 ? "NEW" : "OLD" %>' > </asp:Label>
Thanks in advance.
BB
<asp:Label
ID="lblAsgn"
runat="server"
Text='<%# FormatText(Eval("StatusId")) %>' />
where FormatText could be a method in your code behind:
protected string FormatText(object o)
{
int value;
if (int.Parse(o as string, out value) && value == 0)
{
return "NEW";
}
return "OLD";
}
Try this :
<asp:Label ID="lblAsgn" runat="server" Text= '<%# Eval("StatusId").Equals(0) ? "NEW" : "OLD" %>' > </asp:Label>

Resources