DataBinder conditions

A

Aleko Petkov

Hi,

I want to display different content in a DataGrid row, depending on the
value returned by a DataBinder. I am trying to write something like this:

<ItemTemplate>
<% if( DataBinder.Eval(Container.DataItem, "UserID") > 0 ) { %>
<asp:Hyperlink ... />
<% } else { %>
<asp:Literal ... />
% } %>
</ItemTemplate>

Basically if the current row's UserID field is greater than zero, display a
hyperlink. Otherwise, just display some static text.

If I run this, ASP.NET complains that it doesn't know what Container is. If
I add a # just after the first <% tag, it then says that if statements are
illegal.

So,
1. Is it possible to do this, or do I have to move the DataBinder code to
function in the code-behind?
2. What is the # sign for, anyway? I can't find anything information about
it in the documentation.

Thanks,

Aleko
 
E

Eliyahu Goldin

Aleko,

You should put all controls in the ItemTemplate and databind Visible
property of the controls you want to show/hide to UserID field. In the
databind expression you can check for >0. You don't have to use codebehind
for this. # sign is a part of databind expression syntax. Look in MSDN
library for databind expressions and their syntax.

Eliyahu
 
Y

Yunus Emre ALPÖZEN

Firstly, # sign means that code will be interpreted at the time of
databinding.

I can advise you to put your condtion on both control Visible Attributes.
For hyperlink visible attribute write <%# (
DataBinder.Eval(Container.DataItem, "UserID") > 0 ) %>
For literal visible attribute write <%# !(
DataBinder.Eval(Container.DataItem, "UserID") > 0 ) %>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top