Why visible=false only work sometimes on datagrid?

A

Adam Smith

The following code in my onitemcreated works for some item rows in my
datagrid, but not others. I am trying to set a linkbutton visible for
downloading a file if the data is available in the database, otherwise
set a "not available" label visible. The outcome is that for the first
row on my datagrid, the linkbutton is visible, for the second row both
the linkbutton *and* the label are visible and for the third row, again
only the linkbutton is visible:

private void OnItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
if(DataBinder.Eval(e.Item.DataItem,"RESULT") == DBNull.Value)
((LinkButton)e.Item.Cells[10].FindControl("lbtnDownload")).Visible =
false;
else
((Label)e.Item.Cells[10].FindControl("lblNotAvailable")).Visible =
false;
}
}

Thanks in advance.

Adam Smith
 
J

Jeffrey A. Voigt

Your code is executing for all ListItemType.Item types ... you have to also
include the ListItemType.AlternatingItem type!

Thanks,
- jeff
 
M

Matt Berther

Hello Adam,

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
private void OnItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
if(DataBinder.Eval(e.Item.DataItem,"RESULT") == DBNull.Value)
((LinkButton)e.Item.Cells[10].FindControl("lbtnDownload")).Visible =
false;
else
((Label)e.Item.Cells[10].FindControl("lblNotAvailable")).Visible =
false;
}
}
 
A

Adam Smith

Thanks!

Matt Berther said:
Hello Adam,

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
private void OnItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
if(DataBinder.Eval(e.Item.DataItem,"RESULT") == DBNull.Value)
((LinkButton)e.Item.Cells[10].FindControl("lbtnDownload")).Visible =
false;
else
((Label)e.Item.Cells[10].FindControl("lblNotAvailable")).Visible =
false;
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top