datalist的最后数据为空 [英] Last data of datalist is empty

查看:70
本文介绍了datalist的最后数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码更改datalist中的标签值,但是当我运行项目时,所有这些都是正确的,但最后一个数据是空的。

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.

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();

  }

}

推荐答案

hi


datalist.itemDatabound当项目是绑定到DataList控件的数据时发生。虽然datalist.items在绑定数据后可用。(所以当最后一行绑定时,你得到前一行数据。所以最后一行数据保持不变。)



所以你不需要编写foreach,因为datalist.itemdatabound是在每一行表id绑定到datalist时发生的。



相反你可以使用类似下面的代码



protected void dlProducts_ItemDataBound(object sender,DataListItemEventArgs e)

{



DataListItem li = e.Item;

标签lbl1 =(标签)li.FindControl(Label1);

lbl1.Text =***;

标签lblt =(标签)li.FindControl(lblprice);

lblt.Text =replace;



}
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";

}


这篇关于datalist的最后数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆