Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1009869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:59:41+00:00 2026-05-16T08:59:41+00:00

<% if(Eval(SaveDate) != DBNull.Value){ %> do magic <%} %> gives me error: Databinding methods

  • 0
 <% if(Eval("SaveDate") != DBNull.Value){ %>
     do magic                           
 <%} %>

gives me error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I could write : <%# Eval(“SaveDate”) != DBNull.Value ? do magic
But I need to do lots of html magic in if statement.

I know I should add # in order to use Eval, but not sure about correct syntax.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:59:42+00:00Added an answer on May 16, 2026 at 8:59 am

    One solution is to wrap the content in a runat=”server” tag with a Visible value, e.g.,

    <div runat="server" Visible='<%# Eval("SaveDate") != DBNull.Value %>'>
       do magic
    </div>
    

    div can be any HTML tag, but <asp:Panel> and <asp:PlaceHolder> could also be used. Note that “do magic” is still databound, so it’s not a perfect solution if it contains expensive code or code that could generate an error if Eval("SaveDate") == DBNull.Value.

    Note that Visible="false" will omit the tag and all its contents from the generated HTML, this means that it is very different from style="display:none" or style="visible:hidden", so don’t worry about that.

    But, if your “do magic” is reasonably complex, another rather simple solution (a bit of a hack) is: use a Repeater (or FormView) with its DataSource set to an array of one item (visible) or no items (hidden):

    <asp:Repeater runat="server" DataSource='<%# ElementIfTrue(Eval("SaveDate") != DBNull.Value) %>'
        <ItemTemplate>
            do magic
        </ItemTemplate>
    </asp:Repeater>
    
    protected IEnumerable ElementIfTrue(bool condition) 
    {
        if (condition)
            return new object[] { Page.GetDataItem() };
        else
            return new object[0];
    }
    

    The actual contents of the datasource array is either empty (hidden) or the element you were already binding to. This makes sure you can still call <%# Eval(...) %> inside the ItemTemplate.

    With this approach, your “do magic” is a template which will only be executed if DataSource has one or more items. Which is taken care of by ElementIfTrue. It’s a bit of a mind bender, but it can save you every once in a while.

    As a side note: packing your “do magic” in a user control can also keep the complexity down. You don’t really need to change a thing in your HTML/ASP.NET tag mix (<%# Eval("...") %> still works even inside a user control).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can i save the returning value of eval function in a variable and use
I was wondering if eval (or some variant of jQuery's getScript) can be used
I have seen bind and eval used interchangeably especially in ASP.NET GridViews. What is
I've never used eval() before, so I assume that I just got the syntax
Question: I'm using eval to parse a JSON return value from one of my
eval() returns the same error, even with the simplest of inputs. >> ns=int2str(17) >>
I download the poker-eval library, a open-source resource for poker. I can't install, and
Is it possible using Eval? If so, can someone post a short fragment to
When calling eval in Ruby you can specify a filename to display in errors
I'm used to using eval() in languages like javascript e.t.c. In cocos2d, to select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.