• repeater中的item databound事件的具体发生过程


    在用repeater的item databound时遇到了一些蜜汁问题

    其原因还是对这个事件理解不够透彻。

    首先我们从字面上理解一下:item是分列,条目;data是数据; bound 是绑定。

    该事件在repeater控件中的某一项被数据绑定后但尚未呈现在页面上之前发生。即每次绑定数据后都会触发这个事件。

    而repeater.item是指已经绑定并显现出来的数据

    举个例子:我想用databound事件来遍历repeater中的控件,依此设置它的属性

    protected void blog_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {

       for (int i = 0; i < blog.Items.Count; i++)
                    {
                        LinkButton report =(LinkButton)blog.Items[i].FindControl("report");  //找到repeater blog 中的所有转发的控件
    
                        report.Visible = false;
                    }
    }

    }

    这个代码的实现其实是这样子的:(设blog中的数据总共有3条)

    绑定第一条数据时:此时的由于数据未显示出来,blog.item.count==0,所以循环不执行

    绑定第二条数据时:此时items.count==1;只执行一次循环

    绑定第三条数据时:此时items.count==2;执行两次循环

    最后数据绑定完成,不会再触发databound事件

    这个时候最后的一个控件的属性永远获取不到。

    那么我们怎么解决这个问题呢?

    方法一:

    在绑定并显示出全部数据后,再对repeater中的最后一个控件进行编辑(此时需要属于判定repeater中是否有数据,即判定items.count是否为0)

                                blog.DataBind();
    
                                if (blog.Items.Count > 0)
                                {
                                    LinkButton report = (LinkButton)blog.Items[photo.Items.Count - 1].FindControl("report");  //找到repeater blog 中的最后一个转发的控件
    
                                    report.Visible = false;
                    }

      

    方法二:

    则是在每次触发databound事件时对其每一个控件进行编辑

        protected void blog_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             ((LinkButton)e.Item.FindControl("report")).Visible = false;  //找到repeater blog 中的每个转发的控件
      }
    

      

    就这样~

  • 相关阅读:
    HDU 3091 Necklace <<状压dp
    HDU 1074 Doing Homework<<状压dp
    单片机程序设计中的“分层思想”
    单片机的 FIFO循环队列实现
    串口多字节接收
    单片机多字节串口接收(转)
    SD卡应用总结(Fatfs)
    FATFS 初学之 磁盘 I/O接口
    FATFS 初学之 f_gets/ f_putc/ f_puts/ f_printf
    FATFS 初学之 f_chdir/ f_chdrive
  • 原文地址:https://www.cnblogs.com/ivan99/p/6138482.html
Copyright © 2020-2023  润新知