Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using '<%# Eval("item") %>'; Handling Null Value and showing 0 against

If dataitem is Null I want to show 0

<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label> 

How can I accomplish this?

like image 822
Muhammad Akhtar Avatar asked Dec 30 '09 11:12

Muhammad Akhtar


People also ask

What is the use of <%?

The <%-- --%> Tags are used for specifying Server side comments in aspx page.

What does <% %> mean in HTML?

Summary. The new <%: %> syntax provides a concise way to automatically HTML encode content and then render it as output. It allows you to make your code a little less verbose, and to easily check/verify that you are always HTML encoding content throughout your site.

What does<% mean in ASP net?

It means you're binding some data element.

What is<%= in ASP net?

<%$ %> is an ASP.NET Expression Builder. Used for runtime expression binding for control properties through the server tag attributes. Used with AppSettings , ConnectionStrings , or Resources (or your own custom extension, for example to use code-behind properties).


2 Answers

You can also create a public method on the page then call that from the code-in-front.

e.g. if using C#:

public string ProcessMyDataItem(object myValue) {   if (myValue == null)   {      return "0 value";   }    return myValue.ToString(); } 

Then the label in the code-in-front will be something like:

<asp:Label ID="Label18" Text='<%# ProcessMyDataItem(Eval("item")) %>' runat="server"></asp:Label> 

Sorry, haven't tested this code so can't guarantee I got the syntax of "<%# ProcessMyDataItem(Eval("item")) %>" entirely correct.

like image 67
Jason Snelders Avatar answered Oct 10 '22 11:10

Jason Snelders


I'm using this for string values:

<%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%> 

You can also use following for nullable values:

<%#(Eval("Data") == null ? "0" : Eval("Data"))%> 

Also if you're using .net 4.5 and above I suggest you use strongly typed data binding:

<asp:Repeater runat="server" DataSourceID="odsUsers" ItemType="Entity.User">     <ItemTemplate>         <%# Item.Title %>     </ItemTemplate> </asp:Repeater> 
like image 32
HasanG Avatar answered Oct 10 '22 09:10

HasanG