Binding a value in asp.net - 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

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

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>

Set value for 'visible' property in ASPX page programatically

I am trying to set the visible property for a label to either true or false depending on a condition. This is in ASPX page. I am doing something wrong and getting error when this is executed.
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED'
Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") > 0%>'>
</asp:Label></td>
Error I am getting is below.
Compiler Error Message: CS0019: Operator '>' cannot be applied to
operands of type 'object' and 'int'
What changes need to be done?
All I need to do set the visible property of the LABEL to true when 'IsAuthorized' is greater than zero.
That's because you have a syntax error, you silly bunny.
Here you are, it should be like this:
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# DataBinder.Eval(Container.DataItem, "IsAuthorized") %>' /></td>
You had an extra > and a 0 in there somewhere.
Also, since you aren't doing anything between the <asp:Label and </asp:Label>, you can close it with an end slash and skip a separate ending tag. Like this <asp:Label ... />
ALSO, sometimes trying to set a visible property like that causes problems, the program can complain that the value wasn't a Boolean. You might want to also ad an explicit conversion like this:
Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "IsAuthorized")) %>'
Assuming that IsAuthorized is a bit type, just cast it to a boolean:
Visible='<%#Convert.ToBoolean(Eval("IsAuthorized"))%>'
Note on a server side control you can do this:
<someControl id="myId" runat="server" Visible='<%# this.SomeField > 5 %>'>
But it won't work unless you call DataBind in the code behind, such as in Page_Load:
myId.DataBind():
Assuming IsAuthorized is an integer, you should use this:
Visible='<%# ((int)DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>'
Eval returns an object, so you have to cast it to an integer first.
<td><asp:Label ID="Label23" runat="server" Text='CERTIFIED' Visible='<%# (int)(DataBinder.Eval(Container.DataItem, "IsAuthorized")) > 0 %>' ></asp:Label></td>

Resources