Displaying databound values as members of a collection or array

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I am using databinding and the Repeater control to display the results of a
database query. I normally use the DataBinder.Eval method for this, but in a
scenario I am currently dealing with I would like to be able to do something
like one of the following:


If the value from the database is True then display 'Active', if it is False
display 'Inactive'

OR

Use the value from the database as an index or key to get a value from an
array, collection, or enumeration, which will be the displayed value


I realize that both of these can easily be done using the ItemDataBound
event, but since they are very simple operations I was wondering if there is
a more efficient technique than calling ItemDataBound for every record. Any
ideas? Thanks.

Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
 
You can create a mini "display value" function on the page code behind.


public string DisplayMyCoolValue( object theValue )
{

if (null== theValue)
{
return string.Empty;
}

bool b = Convert.ToBoolean(theValue);
if(b)
{
return "Active";
}
return "Inactive";

}


notice the "theValue" goes in as on object. That's because of
DataBinder.Eval .

Now just take your DataBinder.Eval , and wrap the function call around
(outside of) it.

DisplayMyCoolValue ( DataBinder.Eval ("ItemActive" )

You'll have to play with it a little, I'm coding from memory.
 
Here's the call to set the text of a label:

Text='<%# DisplayMyCoolValue(DataBinder.Eval(Container,
"DataItem.ItemActive") )%>'
 
Back
Top