Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am using this code for changing a label value in datalist but when I run the project all of them is correct but the last data is empty.
C#
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) {

  javidDataContextdc=new javidDataContext();
  foreach (DataListItem li in DataList1.Items)
  {

    string itemID = DataList1.DataKeys[0].ToString();

    Label lbl1 = (Label)li.FindControl("Label1");

    Label lblt = (Label)li.FindControl("lblt");
    lblt.Text = (from p in dc.tbl_writer_translators where p.wid == int.Parse(lbl1.Text) select p.fullname).First();

  }

}
Posted
Comments
DamithSL 21-May-14 14:08pm    
what is empty? any exceptions?
Sunasara Imdadhusen 22-May-14 2:01am    
doesw in every foreach itemID will have a value?

1 solution

hi
datalist.itemDatabound Occurs when an item is data bound to the DataList control. while datalist.items are available after data is bound.(so when the last row was binding you get the previous row data.so the last data remains unchanged.)

so you don't need to write foreach because datalist.itemdatabound occurs when each row of table id binding to datalist.

Instead you can use like the below code

protected void dlProducts_ItemDataBound(object sender, DataListItemEventArgs e)
{

DataListItem li = e.Item;
Label lbl1 = (Label)li.FindControl("Label1");
lbl1.Text = "***";
Label lblt = (Label)li.FindControl("lblprice");
lblt.Text = "replace";

}
 
Share this answer
 
Comments
Member 10313011 22-May-14 15:17pm    
thanks for your answer but i have same problem yet

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