repeat 的两个事件ItemDataBound和ItemCommand

ItemDataBound主要是用来最后改变一次repeat在客户端加载的数据主要用来设置 ItemCommand主要是利用repeat的按钮控件的CommandName和CommandArgument

1.ItemCommand实例

         protected void Repeater3_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "启用")//CommandName主要使前台代码和后台代码打交道
            {
                T_UsersTableAdapter adapter = new T_UsersTableAdapter();
                long id=  Convert.ToInt64(e.CommandArgument);//CommandArgument主要使后台与数据库打交道
                adapter.AlterjiyongById(id);
                Repeater3.DataBind();
            }
            else if (e.CommandName == "禁用")
            {
                T_UsersTableAdapter adapter = new T_UsersTableAdapter();
                long id = Convert.ToInt64(e.CommandArgument);
                adapter.AlterqiyongByID(id);
                Repeater3.DataBind();

            }
            else {
                throw new Exception("有错");
            }
        }

2. ItemDataBound实例

protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//指触发对象的类型是DadaList里的基本行或是替换行(简单的说是Repeater里的所有数据项内容)          

         {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                var row = (WebApplication1.ObjDateScr.DAL.DataSetPerson.T_UsersRow)drv.Row;
                if (row.state == "禁用")
                {
                    HtmlTableRow trRow = (HtmlTableRow)e.Item.FindControl("trRow");
                    trRow.Attributes["class"] = "trRow";//给<tr runat="server" class="trRow">的行添加属性
                    Button btn = (Button)e.Item.FindControl("disable");
                    btn.Visible = false;
                    Button btn2 = (Button)e.Item.FindControl("able");
                    btn2.Visible = true;

                }
                else if (row.state == "启用")
                {
                    HtmlTableRow trRow = (HtmlTableRow)e.Item.FindControl("trRow");
                    trRow.Attributes["class"] = "";
                    Button btn = (Button)e.Item.FindControl("disable");
                    btn.Visible = true;
                    Button btn2 = (Button)e.Item.FindControl("able");
                    btn2.Visible = false;

                }
                else
                {
                    throw new Exception("非法的状态");

                }
            }
        }

posted @ 2011-06-10 02:06  haiziguo  阅读(446)  评论(0编辑  收藏  举报