Displaying databound values as members of a collection or array

Discussion in 'ASP .Net' started by Nathan Sokalski, Aug 13, 2008.

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

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Aug 13, 2008
    #1
    1. Advertising

  2. Nathan Sokalski

    sloan Guest

    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.





    "Nathan Sokalski" <> wrote in message
    news:uG$4d6X$...
    >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
    >
    > http://www.nathansokalski.com/
    >
     
    sloan, Aug 13, 2008
    #2
    1. Advertising

  3. Nathan Sokalski

    sloan Guest

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

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




    "sloan" <> wrote in message
    news:OpBDQKY$...
    >
    >
    >
    > 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.
    >
    >
    >
    >
    >
    > "Nathan Sokalski" <> wrote in message
    > news:uG$4d6X$...
    >>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
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
     
    sloan, Aug 13, 2008
    #3
    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. JFCM
    Replies:
    4
    Views:
    5,741
  2. Øyvind Isaksen
    Replies:
    1
    Views:
    983
    Øyvind Isaksen
    May 18, 2007
  3. rodchar
    Replies:
    0
    Views:
    397
    rodchar
    Nov 26, 2007
  4. Manu
    Replies:
    0
    Views:
    171
  5. Nathan Sokalski

    Displaying databound values as members of a collection or array

    Nathan Sokalski, Aug 13, 2008, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    109
    sloan
    Aug 13, 2008
Loading...

Share This Page