zoukankan      html  css  js  c++  java
  • 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 中的每个转发的控件
      }
    

      

    就这样~

  • 相关阅读:
    MySQL8 Keepalived+双主
    Last_SQL_Errno: 1050 Last_SQL_Error: Error 'Table 'events' already exists' on query. Default database: 'eygle'. Query: 'create table events
    [MY-011522] [Repl] Plugin group_replication reported: 'The member contains transactions not present in the group.
    MYSQL8 裸机搭主从
    MY-011292 MY-011300 MY-013597 MY-011300
    RHLE8 docker安装
    docker harbor x509: certificate signed by unknown authority action: push: unauthorized to access repository
    harbor配置https访问
    action: push: unauthorized to access repository
    unlock DDIC for HANADB
  • 原文地址:https://www.cnblogs.com/ivan99/p/6138482.html
Copyright © 2011-2022 走看看