Skip to content
March 19, 2008 / kiranpatils

IF…else with Eval is not supported+GridView+ASP.NET

Yesterday i was struggling with GridView I need to check some if..else logic and based on it have to show values in Grid..I am checking it in Eval if else but can’t get succeeded because if..else is not supprted with Eval..then how i have achieved it..its concept of “Binding Grid with functions”. Let us see..


On binding of Grid i want to check that if entityCount>1 then have to show that value in grid with HyperLink which points to some page else show Plain Text in grid..

I was trying this one[Which won’t work]

<asp:TemplateField HeaderText=”Def Price”>
<%# if Eval(“entityPriceCountField”) > 1%>
<asp:HyperLink ID=”lnkentityPrice” Text='<%# Eval(“entityPriceField”) %>’ runat=”Server”   Target=”_blank” NavigateUrl=”#”/>
<%# else  %>
<asp:Label ID=”lblentityPrice” Text='<%# Eval(“entityPriceField”) %>’ CssClass=”SearchBookingsLabelStyle” runat=”Server” />


NOTE: You can’t write like this.


I had its solution:

1. Binded my grid with function in my .aspx.cs file.

this function check count if count>1 then text will be in hyper link else show plain text.

2.  Called it witin my Grid.

Here it is:

.aspx page 

<asp:TemplateField HeaderText=”Def Price”>
<asp:Label ID=”entityPrice” Text='<%# GetEntityPriceText((double)Eval(“entityPriceField”),(int)Eval(“entityPriceCountField”))%>’ CssClass=”SearchBookingsLabelStyle” runat=”Server” />

 .aspx.cs code

 protected string GetEntityPriceText(Nullable<double> entityprice,Nullable<int> entityPriceCount)
//If count>1 then show text in HyperLink
string labelText = string.Empty;
if (entityPriceCount > 1)
labelText = “<a href=MyPage.aspx’>” + entityprice.ToString() + “</a>”;
labelText = entityprice.ToString();
eturn labelText;

It worked like a chram!!!



Leave a Comment
  1. devio / Mar 19 2008 5:41 pm

    a HyperLink behaves like a Label if you don’t provide a NavigateUrl.
    Therefore it would be “more beautiful” to define an asp:HyperLink with NavigateUrl=” and return a null string in GetNavigateUrl() if PriceCount < 1, dropping the Label altogether

  2. devio / Mar 19 2008 5:42 pm

    aaaarg should be NavigateUrl='<# GetNavigateUrl(params) #>’

  3. Baljeet / Jul 12 2010 11:37 am

    Nice article dear…………..It’s working fine

    • kiranpatils / Jul 20 2010 5:11 pm

      Thanks Baljeet!

  4. Asanka / Apr 26 2011 10:21 am

    Thanks for the tip…
    really helped me.I also want to do a similar thing. and it works fine.

  5. Debojyoti Singha Roy / Feb 4 2016 12:00 pm

    Your code really worked like a charm…..Thank You very much


  1. 2010 in review « A Place for C Sharpers/.Netters

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: