Repeater的应用之嵌套和行操作

    技术2022-05-11  14

    repeater是一个 轻量级的控件,它具有效率高,使用灵活等特点,可以根据用户的不同需求生成比较复杂的界面。以下是一个简单的例子,通过嵌套实现常见的子报表功能。同时也顺便提了一下对它的基本数据的操作。

    请看示例(asp 2.0):

    aspx:

     

    < asp:Repeater  ID ="rp1"  runat ="server" >              < HeaderTemplate >              < table  width ="100%"  border ="1"  style ="border-collapse:collapse;font-size:12px" >                  < tr >                      < th  align ="center"  colspan ="3" > 总门员工表 </ td >                  </ tr >                  < tr >                     < th  > 部门编号 </ th >                         < th   colspan ="2" > 部门名称 </ th >                                         </ tr >                                      </ HeaderTemplate >          < ItemTemplate >                 < tr  bgcolor ="#336699"  style ="color:White" >                      < td > <% # Eval ( " id " ) %> </ td >                      < td  colspan ="2" > <% # Eval ( " deptname " ) %> </ td >                                     </ tr >                < tr >                   < td ></ td >                  < td >                      < asp:Repeater  ID ="rp2"  runat ="server"                                          DataSource ='<%#((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("RelationName")% > '                                           OnItemCommand="rp2_ItemCommand"                     >                                                 < HeaderTemplate >                               < table  width ="100%"  cellspacing ="0"  border ="0"   cellpadding ="0" >                                                         < tr   bgcolor ="#eeeef1"  style ="color:black" >                                 < td  align ="center" > 删除 </ td >                                 < td > 员工编号 </ td >                                     < td > 员工名称 </ td >                                 </ tr >                                                      </ HeaderTemplate >                          < ItemTemplate >                               < tr >                                  < td  align ="center" >                                      < asp:LinkButton  ID ="nkbDelete"  OnClientClick ="javascript:return confirm('真的要删除吗?');"  runat ="server"  Text ="删除"  CommandName ="delete"  CommandArgument ='<%#Eval("[id]")% > '> </ asp:LinkButton >                                      < asp:CheckBox  ID ="chkSel"  runat ="server"  OnCheckedChanged ="CheckBoxChanged"  KeyID ='<%#Eval("[id]")% > ' AutoPostBack="true"    />                                     < asp:Button  ID ="btnDelete"  runat ="server"  Text ="删除"    CommandName ="delete"  CommandArgument ='<%#Eval("[id]")% > ' /> < label  id ="divDelete"    style ="border:solid 1px black;width:60px;"  onclick ="javascript:this.previousSibling.click();" > 删除 </ label >                                  </ td >                                  < td > <% #DataBinder.Eval(Container.DataItem, " [id] " ) %>                                  < td > <% # Eval ( " [empname] " ) %> </ td >                               </ tr >                          </ ItemTemplate >                          < FooterTemplate >                              </ table >                          </ FooterTemplate >                      </ asp:Repeater >                                      </ td >                </ tr >                         </ ItemTemplate >          < FooterTemplate >                </ table >          </ FooterTemplate >      </ asp:Repeater >

     

    后台程序:

     

      protected   void  Page_Load( object  sender, EventArgs e)    {                           if  ( ! this .IsPostBack)        {             this ._BindRepeater();        }             }    DataSet _GetDataSet()    {         if  (ViewState[ " ds " !=   null )        {             return  (DataSet)ViewState[ " ds " ];        }                DataSet ds  =   new  DataSet();        DataTable dt  =   new  DataTable();        dt.Columns.Add( " id " typeof ( int ));        dt.Columns.Add( " DeptName " );        ds.Tables.Add(dt);        dt.Rows.Add( 1 " 市场部 " );        dt.Rows.Add( 2 " 人力资源部 " );        dt.Rows.Add( 3 " 金融事业部 " );        dt.Rows.Add( 4 " 通信事业部 " );        dt  =   new  DataTable();        dt.Columns.Add( " id " typeof ( int ));        dt.Columns.Add( " deptid " typeof ( int ));        dt.Columns.Add( " empname " );         for  ( int  i  =   0 ; i  <   20 ; i ++ )        {            dt.Rows.Add(i, i  %   4   +   1 " 员工 "   +  i.ToString());        }        ds.Tables.Add(dt);         /// 添加主父子关系         ds.Relations.Add( " RelationName " , ds.Tables[ 0 ].Columns[ " id " ], ds.Tables[ 1 ].Columns[ " deptId " ]);        ViewState[ " ds " =  ds;         return  ds;    }     void  _SaveData(DataSet ds)    {        ViewState[ " ds " =  ds;    }     void  _DeleteData( string  sID)    {        DataSet ds  =   this ._GetDataSet();        DataTable dt  =  ds.Tables[ 1 ];              DataRow[] row  =  dt.Select( " id= "   +  sID);         if  (row.Length  >=   1 )        {            row[ 0 ].Delete();        }         this ._SaveData(ds);    }     void  _BindRepeater()    {        DataSet ds  =   this ._GetDataSet();               this .rp1.DataSource  =  ds.Tables[ 0 ].DefaultView;         this .rp1.DataBind();      }     protected   void  CheckBoxChanged( object  sender,EventArgs e)    {        CheckBox cb  =  (CheckBox)sender;         string  sID  =  cb.Attributes[ " KeyID " ];         this ._DeleteData(sID);         this ._BindRepeater();    }         protected   void  rp2_ItemCommand( object  sender,RepeaterCommandEventArgs e)    {                if  (e.CommandName  ==   " delete " )        {             string  sID  =  e.CommandArgument.ToString();             this ._DeleteData(sID);             this ._BindRepeater();                    }    }

     

    说明:对于repleater的操作。我用了四个基本元素来实现。linkbutton 和button是一样通过指定commandname触发ItemCommand事件,并绑定了CommandArgument以便在后台事件中获取。

    而CheckBox并没有commandname和CommandArgument属性,于是我给它指定了一个自定义的属性KeyID,并绑定数据的id字段。然后在checkbox的事件中获取它的属性进行相关数据操作。

    label的操作,这就借用了button的事件来实现。这里label可换成任何可显示在网页中的html元素。


    最新回复(0)