DataBinder conditions

  • Thread starter Thread starter Aleko Petkov
  • Start date Start date
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
 
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
 
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 ) %>
 
Back
Top