Displaying databound values as members of a collection or array

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/
 
S

sloan

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.
 
S

sloan

Here's the call to set the text of a label:

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top