OnItemDatabound help

  • Thread starter Thread starter Spondishy
  • Start date Start date
S

Spondishy

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.
 
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
 
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!
***************************
 
Thanks for the response... I knew I'd got this working before.

Appreciated.

Karl said:
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 said:
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.
 

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

Back
Top