OnItemDatabound help

Discussion in 'ASP .Net' started by Spondishy, Oct 31, 2005.

  1. Spondishy

    Spondishy Guest

    Hi,

    I have a datalist control, and in each row I have a label and a
    hyperlink control. The label is bound to an id field in the db.

    What I'm trying to do is hide the hyperlink if the id is a certain
    value.

    My question is, does OnItemDataBound fire before actually binding the
    controls, as it appears the label has no text (although it should), so
    the hyperlink never disappears.

    Is there another way of doing this?

    Thanks.
     
    Spondishy, Oct 31, 2005
    #1
    1. Advertising

  2. Spondishy

    Karl Seguin Guest

    Ya, ur going about it just a little wrong.

    Instead of doing:

    Label l = (Label)e.Item.FindControls("someId");
    if (l.Text == "0")
    {
    //hide
    }

    use the e.Item.DataItem property which gives you access the the underlying
    data being bound. So if you are binding to a dataset/dataview/datatable,
    you would do:

    int id = Convert.ToInt32(((DataRowView)e.Item.DataItem)["SomeId"]);
    if (id == 0)
    {
    e.Item.FindControls("link").Visible = false;
    }

    Karl

    --

    MY ASP.Net tutorials
    http://www.openmymind.net/

    "Spondishy" <> wrote in message
    news:...
    > Hi,
    >
    > I have a datalist control, and in each row I have a label and a
    > hyperlink control. The label is bound to an id field in the db.
    >
    > What I'm trying to do is hide the hyperlink if the id is a certain
    > value.
    >
    > My question is, does OnItemDataBound fire before actually binding the
    > controls, as it appears the label has no text (although it should), so
    > the hyperlink never disappears.
    >
    > Is there another way of doing this?
    >
    > Thanks.
    >
     
    Karl Seguin, Oct 31, 2005
    #2
    1. Advertising

  3. In addition to Karl's answer, there is another way. You can build the entire
    "data grid" from scratch in code behind. I would only go this route, however,
    if it is absolutely necessary, as it is a pain.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ***************************
    Think Outside the Box!
    ***************************


    "Spondishy" wrote:

    > Hi,
    >
    > I have a datalist control, and in each row I have a label and a
    > hyperlink control. The label is bound to an id field in the db.
    >
    > What I'm trying to do is hide the hyperlink if the id is a certain
    > value.
    >
    > My question is, does OnItemDataBound fire before actually binding the
    > controls, as it appears the label has no text (although it should), so
    > the hyperlink never disappears.
    >
    > Is there another way of doing this?
    >
    > Thanks.
    >
    >
     
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Oct 31, 2005
    #3
  4. Spondishy

    Spondishy Guest

    Thanks for the response... I knew I'd got this working before.

    Appreciated.

    Karl Seguin wrote:
    > Ya, ur going about it just a little wrong.
    >
    > Instead of doing:
    >
    > Label l = (Label)e.Item.FindControls("someId");
    > if (l.Text == "0")
    > {
    > //hide
    > }
    >
    > use the e.Item.DataItem property which gives you access the the underlying
    > data being bound. So if you are binding to a dataset/dataview/datatable,
    > you would do:
    >
    > int id = Convert.ToInt32(((DataRowView)e.Item.DataItem)["SomeId"]);
    > if (id == 0)
    > {
    > e.Item.FindControls("link").Visible = false;
    > }
    >
    > Karl
    >
    > --
    >
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    > "Spondishy" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I have a datalist control, and in each row I have a label and a
    > > hyperlink control. The label is bound to an id field in the db.
    > >
    > > What I'm trying to do is hide the hyperlink if the id is a certain
    > > value.
    > >
    > > My question is, does OnItemDataBound fire before actually binding the
    > > controls, as it appears the label has no text (although it should), so
    > > the hyperlink never disappears.
    > >
    > > Is there another way of doing this?
    > >
    > > Thanks.
    > >
     
    Spondishy, Oct 31, 2005
    #4
    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. Antonio D'Ottavio
    Replies:
    1
    Views:
    2,735
    Antonio D'Ottavio
    Sep 19, 2005
  2. Steven
    Replies:
    1
    Views:
    1,651
    Simon
    Jul 9, 2003
  3. Maureen Black

    OnItemDataBound issue...

    Maureen Black, Apr 4, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    405
    Maureen Black
    Apr 4, 2004
  4. Replies:
    0
    Views:
    890
  5. Joey
    Replies:
    0
    Views:
    142
Loading...

Share This Page