最近使用Repeater的时候遇到一个问题.页面上有一个Repeater,并为其添加了一个ItemCreated事件:

    protected void Repeater2_ItemCreated(object sender, RepeaterItemEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item)

        {

            Label body = e.Item.FindControl("lbBody") as Label;

            string bodyText = ((Post)e.Item.DataItem).Body;

            if (bodyText.Length > 20)

            {

                body.Text = bodyText.Substring(0, 20) + "……";

            }

            else

                body.Text = bodyText;

        }

}

 

目的是为了在显示数据的时候对数据做一点改动.

但是,每当页面回调的时候就会出现问题: DataItem为空值.最后才发现,ItemCreatedItemDataBound是有区别的:

ItemCreated在每次Repeater Render的时候都会调用,ItemDataBound则不一定,只有调用了DataBind(),需要进行数据绑定的时候才会调用.

切记!

相关文章:

  • 2021-08-08
  • 2021-08-27
  • 2022-12-23
  • 2021-09-06
  • 2022-01-26
  • 2021-07-13
  • 2021-07-21
  • 2021-11-13
猜你喜欢
  • 2021-05-25
  • 2021-07-22
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案