Per-Row Function Calls in Data-Bound List Controls

Discussion in 'ASP .Net Web Controls' started by Alex Maghen, Nov 14, 2004.

  1. Alex Maghen

    Alex Maghen Guest

    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
    Alex Maghen, Nov 14, 2004
    #1
    1. Advertising

  2. "Alex Maghen" <> wrote in message
    news:...
    > 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
    John Saunders, Nov 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,859
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    398
    tony vee
    Sep 10, 2004
  3. Mario Krsnic
    Replies:
    0
    Views:
    364
    Mario Krsnic
    Jun 23, 2006
  4. D
    Replies:
    0
    Views:
    197
  5. Bob
    Replies:
    5
    Views:
    247
Loading...

Share This Page