asp.net中DataList控件中的Lable控件中的数据怎么取出来啊?

谢谢啦!!
2025-03-04 17:53:21
推荐回答(4个)
回答1:

如果是要在绑定的时候设置Label的值,那么就在DataBound事件下面加此代码:
在ItemDataBound中取
protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string sValue = ((Label)e.Item.FindControl("lable1")).Text;
}
}

如果是需要对DataList中的数据进行更改,或从中取得值后进行利用,那么就需要在ItemCommand事件中添加了

回答2:

在DataList中没有Cell
在GridView中才有...

回答3:

如果ID不确定
还可以用索引
string sValue = ((Label)e.Item.Cell[索引]).Text;

回答4:

在ItemDataBound中取
protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string sValue = ((Label)e.Item.FindControl("lable1")).Text;
}
}
lable1 这是在DataList 中控件的ID