首页 > 编程 > .NET > 正文

Repeater控件实现编辑、更新、删除操作

2024-07-10 13:28:58
字体:
来源:转载
供稿:网友

如何在Repeater控件中实现像GridView控件一样的编辑、更新、删除功能?

下面给出示例 vs.net2008(C#)下编写

后台.cs代码


 

  1. protected void Page_Load(object sender, EventArgs e) 
  2.     if (!Page.IsPostBack) 
  3.     { 
  4.          BindGrid(); 
  5.     } 
  6.  
  7.  
  8. private void BindGrid() 
  9. {           
  10.      string strSQL = "SELECT * FROM [User]"
  11.      OleDbConnection objConnection = new OleDbConnection(GetStrConnection()); 
  12.      objConnection.Open(); 
  13.      OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection); 
  14.      OleDbDataReader reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection); 
  15.      rptUser.DataSource = reader; 
  16.      rptUser.DataBind(); 
  17.  
  18. protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e) 
  19.     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
  20.     { 
  21.          System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem; 
  22.          int userId = int.Parse(record["UserId"].ToString()); 
  23.          if (userId != id) 
  24.          { 
  25.              ((Panel)e.Item.FindControl("plItem")).Visible = true
  26.              ((Panel)e.Item.FindControl("plEdit")).Visible = false
  27.          } 
  28.          else 
  29.          { 
  30.              ((Panel)e.Item.FindControl("plItem")).Visible = false
  31.              ((Panel)e.Item.FindControl("plEdit")).Visible = true
  32.          }        
  33.     } 
  34.  
  35. protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e) 
  36.      if (e.CommandName == "Edit"
  37.      { 
  38.          id = int.Parse(e.CommandArgument.ToString()); 
  39.      } 
  40.      else if (e.CommandName == "Cancel"
  41.      { 
  42.          id = -1; 
  43.      } 
  44.      else if (e.CommandName == "Update"
  45.      { 
  46.          string name = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtName")).Text.Trim(); 
  47.          string email = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtEmail")).Text.Trim(); 
  48.          string qq = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtQQ")).Text.Trim(); 
  49.          string strSQL = "UPDATE [User] SET Name=@Name,Email=@Email,QQ=@QQ WHERE UserId=@UserId"
  50.          OleDbConnection objConnection = new OleDbConnection(GetStrConnection()); 
  51.          OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection); 
  52.          objCommand.Parameters.Add("@Name", OleDbType.VarWChar); 
  53.          objCommand.Parameters["@Name"].Value = name
  54.          objCommand.Parameters.Add("@Email", OleDbType.VarWChar); 
  55.          objCommand.Parameters["@Email"].Value = email; 
  56.          objCommand.Parameters.Add("@QQ", OleDbType.VarWChar); 
  57.          objCommand.Parameters["@QQ"].Value = qq; 
  58.          objCommand.Parameters.Add("@UserId", OleDbType.Integer); 
  59.          objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString()); 
  60.          objConnection.Open(); 
  61.          objCommand.ExecuteNonQuery(); 
  62.          objConnection.Close(); 
  63.      } 
  64.      else if (e.CommandName == "Delete"
  65.      { 
  66.          string strSQL = "DELETE * FROM [User] WHERE UserId=@UserId"
  67.          OleDbConnection objConnection = new OleDbConnection(GetStrConnection()); 
  68.          OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection); 
  69.          objCommand.Parameters.Add("@UserId", OleDbType.Integer); 
  70.          objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString()); 
  71.          objConnection.Open(); 
  72.          objCommand.ExecuteNonQuery(); 
  73.          objConnection.Close(); 
  74.      } 
  75.  
  76.      BindGrid(); 
  77.  
  78. private string GetStrConnection() 
  79.     return "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("~/Database/test.mdb"); 
  80. 前台.aspx代码 
  81.  
  82. <form id="form1" runat="server"
  83.     <asp:Repeater ID="rptUser" runat="server" onitemcommand="rptUser_ItemCommand"  
  84.         onitemdatabound="rptUser_ItemDataBound"
  85.         <HeaderTemplate> 
  86.             <table width="960" align="center" cellpadding="3" cellspacing="1" style="background-color: #ccc;"
  87.                 <thead style="background-color: #eee;"
  88.                     <tr> 
  89.                         <th width="10%"
  90.                             用户ID 
  91.                         </th> 
  92.                         <th> 
  93.                             用户名 
  94.                         </th> 
  95.                         <th width="22%"
  96.                             邮件 
  97.                         </th> 
  98.                         <th width="20%"
  99.                             QQ 
  100.                         </th> 
  101.                         <th width="15%"
  102.                             注册时间 
  103.                         </th> 
  104.                         <th width="12%"
  105.                             操作 
  106.                         </th> 
  107.                     </tr> 
  108.                 </thead> 
  109.                 <tbody style="background-color: #fff;"
  110.         </HeaderTemplate> 
  111.         <ItemTemplate> 
  112.             <asp:Panel ID="plItem" runat="server"
  113.                 <tr style="text-align: center;"
  114.                     <td> 
  115.                         <%# DataBinder.Eval(Container.DataItem, "UserId")%> 
  116.                     </td> 
  117.                     <td> 
  118.                         <%# DataBinder.Eval(Container.DataItem, "Name")%> 
  119.                     </td> 
  120.                     <td> 
  121.                         <%# DataBinder.Eval(Container.DataItem, "Email")%> 
  122.                     </td> 
  123.                     <td> 
  124.                         <%# DataBinder.Eval(Container.DataItem, "QQ")%> 
  125.                     </td> 
  126.                     <td> 
  127.                         <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%> 
  128.                     </td> 
  129.                     <td> <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>' 
  130.                      CommandName="Edit" Text="编辑"></asp:LinkButton>&nbsp;&nbsp; 
  131.                     <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>' 
  132.                      CommandName="Delete" Text="删除" OnClientClick="return confirm('确定要删除?')"></asp:LinkButton> 
  133.                     </td> 
  134.                 </tr> 
  135.             </asp:Panel> 
  136.             <asp:Panel ID="plEdit" runat="server"
  137.                 <tr style="text-align: center;"
  138.                     <td> 
  139.                         <%# DataBinder.Eval(Container.DataItem, "UserId")%> 
  140.                     </td> 
  141.                     <td> 
  142.                         <asp:TextBox ID="txtName" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>' 
  143.                             runat="server"></asp:TextBox> 
  144.                     </td> 
  145.                     <td> 
  146.                         <asp:TextBox ID="txtEmail" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>' 
  147.                             runat="server"></asp:TextBox> 
  148.                     </td> 
  149.                     <td> 
  150.                         <asp:TextBox ID="txtQQ" Text='<%# DataBinder.Eval(Container.DataItem,"QQ") %>' runat="server"></asp:TextBox> 
  151.                     </td> 
  152.                     <td> 
  153.                         <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%> 
  154.                     </td> 
  155.                     <td> 
  156.                       <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>' 
  157.                      CommandName="Update" Text="更新"></asp:LinkButton>&nbsp;&nbsp; 
  158.                     <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>' 
  159.                      CommandName="Cancel" Text="取消"></asp:LinkButton> 
  160.                     </td> 
  161.                 </tr> 
  162.             </asp:Panel> 
  163.         </ItemTemplate> 
  164.         <FooterTemplate> 
  165.             </tbody></table
  166.         </FooterTemplate> 
  167.     </asp:Repeater> 
  168. </form> 

下载代码示例:PageDemo.RAR

相关文档: Repeater控件的分页实现 Repeater多行间隔显示分隔符的方法 Repeater中嵌套使用Repeater

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表