"Alex Maghen" <> wrote in message
news:E14AB315-0768-46CA-91F0-...
> 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
|