Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written the following code for DataList ItemDataBound event to change my image's visibility.
This code works fine for all the items in the DataList but doesn't give me any output for the last item. The Last Item of DataList remains uneffected. Can anyone suggest what changes should I make to affect my last item of DataList also?

My code is as follows:

C#
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
   {
       int i;
       ////foreach (DataListItem di in DataList1.Items)
       for  (i=0; i < DataList1.Items.Count; i++)
       {
           if (((DataList)(DataList1.Items[i].FindControl("DataList3"))).Items.Count <= 0)
           {
               ((System.Web.UI.WebControls.Image)(DataList1.Items[i].FindControl("Image40"))).Visible = true;

           }
           else
           {
               ((System.Web.UI.WebControls.Image)(DataList1.Items[i].FindControl("Image40"))).Visible = false;
           }
       }
   }
Posted
Updated 25-Aug-11 1:07am
v3

It is not going to help with the issue.
But that if condition can be removed. Just a suggestion.
C#
for  (i=0; i < DataList1.Items.Count; i++)
{
  ((System.Web.UI.WebControls.Image)(DataList1.Items[i].FindControl("Image40"))).Visible = (((DataList)(DataList1.Items[i].FindControl("DataList3"))).Items.Count <= 0);
}
 
Share this answer
 
Comments
Amit7071 23-Oct-12 5:51am    
how to bind the datalist with three Tables ?
Prerak Patel 23-Oct-12 6:10am    
Use JOIN in SQL to get data in single DataTable.
protected void dg_outstation_ItemDataBound(object sender, DataGridItemEventArgs e)
{

{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

DataTable dt2 = dl.filltbl("select * from tbl_city where id=" + Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "city_id").ToString()) + "");
((Literal)e.Item.FindControl("lt_city")).Text = dt2.Rows[0]["city"].ToString();

DataTable dt1 = dl.filltbl("select * from tbl_outstation where id=" + Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "id").ToString()) + "and status=1");
if (dt1.Rows.Count > 0)
{
((LinkButton)e.Item.FindControl("lnk_active")).Visible = false;
((LinkButton)e.Item.FindControl("lnk_suspend")).Visible = true;
((ImageButton)e.Item.FindControl("img_active")).Visible = false;
((ImageButton)e.Item.FindControl("img_suspend")).Visible = true;


}
else
{
((LinkButton)e.Item.FindControl("lnk_active")).Visible = true;
((LinkButton)e.Item.FindControl("lnk_suspend")).Visible = false;
((ImageButton)e.Item.FindControl("img_active")).Visible = true;
((ImageButton)e.Item.FindControl("img_suspend")).Visible = false;}

}
}}

}
 
Share this answer
 
Comments
[no name] 9-Mar-13 11:35am    
Do you really think that he is still waiting after all these years?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900