Per-Row Function Calls in Data-Bound List Controls

A

Alex Maghen

Hi. Say I have a DataReader object with a bunch orf rows. Each row has a text
string column and a boolean column.

When I go to display the data in one of the List Controls (such as the
Repeater or DataGrid), I want the string column to be displayed directly.
BUT, for the boolean column, I'll want to call one of my functions which will
receive both the boolean AND that row's string column data and, after some
parsing and other stuff that that function does, will return a string that
will be used as the Src Url in an Image control on the same row in the list
control.

In other words, I need to perform some slightly more complex manipulation on
a row-by-row basis. Can I do this in the data-bound list controls or do I
have to generate all my own HTML in a loop like in the old days?

Does the answer have something to do with the DataBinder.Eval() function? I
don't know.

Thanks.

Alex
 
J

John Saunders

Alex Maghen said:
Hi. Say I have a DataReader object with a bunch orf rows. Each row has a
text
string column and a boolean column.

When I go to display the data in one of the List Controls (such as the
Repeater or DataGrid), I want the string column to be displayed directly.
BUT, for the boolean column, I'll want to call one of my functions which
will
receive both the boolean AND that row's string column data and, after some
parsing and other stuff that that function does, will return a string that
will be used as the Src Url in an Image control on the same row in the
list
control.

In other words, I need to perform some slightly more complex manipulation
on
a row-by-row basis. Can I do this in the data-bound list controls or do I
have to generate all my own HTML in a loop like in the old days?

Does the answer have something to do with the DataBinder.Eval() function?
I
don't know.

Alex, here's a hint: DataBinder.Eval() _is_ a function call. If
DataBinder.Eval can be called, then so can _your_ function. So, instead of

<asp:image runat="server" id="imgX" ImageUrl="<%#
DataBinder.Eval(Container.DataItem, 'BoolField') %>" />

use

<asp:image runat="server" id="imgX" ImageUrl="<%#
MyFunction(Container.DataItem) %>" />

I don't use DataReaders in DataBinding very often, so I forget what kind of
object Container.DataItem will be in this case (DbDataRecord or
IDataRecord?) but define your function like this and find out:

protected string MyFunction(object data)
{
return data.GetType().FullName;
}

Most likely it will be something that implements IDataRecord, so:

protected string MyFunction(IDataRecord data)
{
string retVal = "images/" + data["stringColumn"].ToString() + ".gif";
if ((bool) data["boolColumn"])
{
retVal += "?size=larger";
}

return retVal;
}

John Saunders
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top