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

  • Home
  • SEARCH
  • 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 1111487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:33:19+00:00 2026-05-17T02:33:19+00:00

In a Asp Net data bound control one can use the nice Eval() syntax:

  • 0

In a Asp Net data bound control one can use the nice Eval() syntax:

<div><%# Eval("MyProp") %></div>

but it’s not possible to combine with a conditional statement is it?:

<% if (Eval("MyProp")!="") { %>
<div><%# Eval("MyProp") %></div>
<%} %>

Would be nice to have that option.

So – my option is to put part of the markup in CodeBehind. I really liked to keep it out of there. Any other possibilities?

  • 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-17T02:33:19+00:00Added an answer on May 17, 2026 at 2:33 am
    <div runat="server" visible='<%# !String.IsNullOrEmpty(Eval("MyProp") as string) %>'>
        <%# Eval("MyProp") %>
    </div>
    

    Edit 2010/9/30

    Now that I have a little time I feel like I should expand on my answer. Especially considering that the question is a duplicate that already has a better answer than my original. So I will tackle the first part of your question:

    Why can you not combine if and eval? The reason is that <% ... %> code blocks are executed during the render phase of the page lifecycle. ASP.NET puts your code (the if statement in this case) into a function, then passes that function as a delegate to SetRenderMethodDelegate. The problem is that Eval evaluates your expression “MyProp” on the object returned by Page.GetDataItem. Page.GetDataItem is a bit of magic that only returns a value if there has been a call to DataBind earlier in the call stack. By the time the render phase comes around, all the data binding has finished. Hence when you call Eval, which implicitly calls Page.GetDataItem, the following error is thrown: “Databinding methods […] can only be used in the context of a databound control.”

    So <% ... %> code blocks won’t work, but what about <%# ... %> code blocks, a.k.a. data-binding expressions? Data-binding expressions do run in the desired part of the page lifecycle—within a call to DataBind. However, the operative word here is expression. An if statement is not an expression. Anything you throw between <%# ... %> will be evaluated by the compiler as an expression. If you put a statement in there, the compiler will throw an error.

    Hence we arrive at the idiom of calling Eval in a data-binding expression on the Visible property of a control. Eval is compatible with data-binding expressions; writing an expression is sufficient to assign the value of a property; and the Visible property is able to achieve an effect similar to that of using an inline if statement.
    Final tip: if you ever end up in a situation where you don’t have any existing control to naturally add a Visible condition to, you can always use <asp:PlaceHolder>, a control that doesn’t add any tags to the markup.

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

Sidebar

Related Questions

What is the best way to create a data bound ASP.NET user control? I
Can anyone recommend a free asp.net control that I can use for the following:
I have been trying to use declarative data bound ASP.NET, and struggling with the
I have added a GridView control on my ASP.net webpage and data bound it
i have a asp.net tree view control and its being data bound to XML
I have a repeater that's bound to a SQL Data Source (using ASP.NET). Are
I have an ASP.NET user control that is used in another use control. The
You are implementing an ASP.NET application that uses data-bound GridView controls in multiple pages.
I have an ObjectDataSource on an ASP.NET WebForms page, that also has a data-bound
Microsoft's introduction to data-binding using the asp:Repeater control gives the syntax to fetch a

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.