suppress unit display in label - asp.net

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

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>

Eval Check String Null or Empty

<asp:TemplateField HeaderText="Customer Account Name">
<ItemTemplate>
<asp:Label ID="lblRecieverClientAccountName" runat="server"
Text='<%#Eval("RecieverClientAccountName").ToString()) ?
String.Empty : 'Invalid Account number'
,"RecieverClientAccountName" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I need to check string is empty and show a custom message .It gives me an error "The server tag is not well formed."
The problem is .ToString(). You should check it before .ToString() function
Try as below
<asp:Label ID="lblRecieverClientAccountName" runat="server"
Text='<%#Eval("RecieverClientAccountName")) ?
String.Empty : 'Invalid Account number'
,"RecieverClientAccountName" %>'></asp:Label>
Or check it for null values.
<asp:Label ID="lblRecieverClientAccountName" runat="server"
Text='<%#Eval("RecieverClientAccountName"))==
null : 'Invalid Account number'
,"RecieverClientAccountName" %>'></asp:Label>
I have not tested it so far.
Edit 1
Try this
<%#(String.IsNullOrEmpty(Eval("RecieverClientAccountName").ToString()) ?...
Similar Question on SO Using '<%# Eval("item") %>'; Handling Null Value and showing 0 against
Try this..
<asp:Label ID="lblRecieverClientAccountName" runat="server"
Text='<%#Eval("RecieverClientAccountName")) ?
"" : 'Invalid Account number'
,"RecieverClientAccountName" %>'></asp:Label>
<asp:TemplateField HeaderText="Customer Account Name">
<ItemTemplate>
<asp:Label ID="lblRecieverClientAccountName" runat="server"
Text='<%#Eval("RecieverClientAccountName") ?
String.Empty : 'Invalid Account number, ' +
Eval("RecieverClientAccountName") %>'></asp:Label>
</ItemTemplate>
You had error: "The server tag is not well formed." because of:
'Invalid Account number'
you have to change it to:
"Invalid Account number"
You can use Eval function like this:
Eval("RecieverClientAccountName") == null ? "" : Eval("RecieverClientAccountName").ToString()
or
Eval("RecieverClientAccountName") == DBNull.Value ? "" : Eval("RecieverClientAccountName").ToString()
Check Eval values Null or Not Null
<%# string.IsNullOrEmpty(Eval("RecieverClientAccountName").ToString())? "Null" : "Values" %>

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>

Binding a value in asp.net

I want to bind the non zero value in the text field.
I have written like this :
<asp:TextBox ID="txtHaulZoneCodeLEM" runat="server" CssClass="cagText" Text='<%# Bind("HaulZoneCodeLEM") %>'></asp:TextBox
How to make sure the value is non-Zero in this field?. I don't want Zero in this text field.
I am tried like this :its giving syntax error : "Identifier expected"
<asp:TextBox ID="txtHaulZoneCodeLEM" runat="server" CssClass="cagText" Text='<%# Eval("HaulZoneCodeLEM") != 0 ? Eval("HaulZoneCodeLEM") : "" %>'></asp:TextBox>
try this
<%# Eval("HaulZoneCodeLEM").ToString().Equals("0") ? ""
:Eval("HaulZoneCodeLEM").ToString() %> hope this will help you

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>

Resources