How do I implement edit, update, delete functions like the GridView control in the Repeater control?
Below is a sample vs.net2008 (C #) to be written. From admin10000.com
Background. CS Code
protected void Page_Load (object sender, EventArgs e) {if (! Page.IsPostBack) {Bindgrid (); }}private void Bindgrid () {String strSQL = "SELECT * FROM [User]"; OleDbConnection objconnection = new OleDbConnection (Getstrconnection ()); Objconnection.open (); OleDbCommand objcommand = new OleDbCommand (strSQL, objconnection); OleDbDataReader reader = Objcommand.executereader (commandbehavior.closeconnection); Rptuser.datasource = reader; Rptuser.databind ();} protected void Rptuser_itemdatabound (object sender, RepeaterItemEventArgs e) {if (E.item.itemtype = = ListItemType.Item || E.item.itemtype = = ListItemType.AlternatingItem) {System.Data.Common.DbDataRecord record = (System.Data.Common . DbDataRecord) e.Item.DataItem; int userId = Int. Parse (record["UserId"). ToString ()); if (userId! = ID) {((panel) E.item.findcontrol ("Plitem")). Visible = true; (panel) E.item.findcontrol ("Pledit"). ViSible = false; } else {((panel) E.item.findcontrol ("Plitem")). Visible = false; (panel) E.item.findcontrol ("Pledit"). Visible = true; }}}protected void Rptuser_itemcommand (object source, RepeaterCommandEventArgs e) {if (E.commandname = = "Edi T ") {id = Int. Parse (E.commandargument.tostring ()); } else if (E.commandname = = "Cancel") {id =-1; } else if (E.commandname = = "Update") {string name = (TextBox) This.rptuser.items[e.item.itemindex]. FindControl ("Txtname")). Text.trim (); string email = ((TextBox) This.rptuser.items[e.item.itemindex]. FindControl ("Txtemail")). Text.trim (); String QQ = ((TextBox) This.rptuser.items[e.item.itemindex]. FindControl ("Txtqq")). Text.trim (); String strSQL = "UPDATE [User] SET name= @Name, email= @Email, qq= @QQ WHERE userid= @UserId"; OleDbConnection objconnection = new OleDbConnection (Getstrconnection ()); OlEdbcommand objcommand = new OleDbCommand (strSQL, objconnection); OBJCOMMAND.PARAMETERS.ADD ("@Name", Oledbtype.varwchar); objcommand.parameters["@Name"]. Value = name; OBJCOMMAND.PARAMETERS.ADD ("@Email", Oledbtype.varwchar); objcommand.parameters["@Email"]. Value = email; OBJCOMMAND.PARAMETERS.ADD ("@QQ", Oledbtype.varwchar); objcommand.parameters["@QQ"]. Value = QQ; OBJCOMMAND.PARAMETERS.ADD ("@UserId", OleDbType.Integer); objcommand.parameters["@UserId"]. Value = Int. Parse (E.commandargument.tostring ()); Objconnection.open (); Objcommand.executenonquery (); Objconnection.close (); } else if (E.commandname = = "Delete") {string strSQL = "Delete * from [User] WHERE userid= @UserId"; OleDbConnection objconnection = new OleDbConnection (Getstrconnection ()); OleDbCommand objcommand = new OleDbCommand (strSQL, objconnection); OBJCOMMAND.PARAMETERS.ADD ("@UserId", oledbtype.iNteger); objcommand.parameters["@UserId"]. Value = Int. Parse (E.commandargument.tostring ()); Objconnection.open (); Objcommand.executenonquery (); Objconnection.close (); } Bindgrid ();} private String Getstrconnection () {return "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ("~/ Database/test.mdb ");}
Foreground. aspx code
<form id= "Form1" runat= "Server" > <asp:repeater id= "rptuser" runat= "Server" onitemcommand= "Rptuser_ ItemCommand "onitemdatabound=" Rptuser_itemdatabound "> <HeaderTemplate> <table width= "960" align= "Center" cellpadding= "3" cellspacing= "1" style= "Background-color: #ccc;" > <thead style= "background-color: #eee;" > <tr> <th width= "10%" > User ID </th> <th> User Name </th& Gt <th width= "22%" > Mail </th> <th widt H= "20%" > QQ </th> <th width= "15%" > Registration time </th> <th width= "12%"> Operations </th> </tr> </ thead> <tbody style= "Background-color: #fff;" > </HeaderTemplate> <ItemTemplate> <asp:panel id= "Plitem" runat= "Server" > <tr style= "Text-align:center;" > <td> <%# databinder.eval (Container.DataItem, "UserId")%> </td> <td> <%# DataBinder.Eval (Container.DataItem, " Name ")%> </td> <td> <%# DataBinder.Eval (Con Tainer. DataItem, "Email")%> </td> <td> <%# databin Der. Eval (Container.DataItem, "QQ")%> </td> <td> &L t;%# DataBinder.Eval (COntainer. DataItem, "Addtime", "{0:YYYY-MM-DD}")%> </td> <td> <asp:linkbutton runat= "Server" id= "Lbtedit" commandargument= ' <%# DataBinder.Eval (Container.DataItem, "UserId")%> ' Commandname= "Edit" text= "editor" ></asp:LinkButton> <asp:linkbutton runat= "Server" id= "Lbtdelete" commandargument= ' <%# DataBinder.Eval (Container.DataItem, " UserId ")%> ' commandname=" delete "text=" delete "onclientclick=" return confirm (' OK to delete? ') ' ></asp:LinkButton> </td> </tr> </asp:Panel> <asp:panel id= "Pledit" runat= "Server" > <tr style= "text-align:center;" > <td> <%# databinder.eval (Container.DataItem, "UserId")%> </td> <td> <asp:textBox id= "txtname" text= ' <%# DataBinder.Eval (Container.DataItem, "Name")%> ' runat= ' server ' & gt;</asp:textbox> </td> <td> <asp:textbo X id= "Txtemail" text= ' <%# DataBinder.Eval (Container.DataItem, "Email")%> ' runat= ' server ' & gt;</asp:textbox> </td> <td> <asp:textbo X id= "txtqq" text= ' <%# DataBinder.Eval (Container.DataItem, "QQ")%> ' runat= ' server ></asp:TextBox> </td> <td> <%# DataBinder.Eval (Container.DataItem, "Add Time "," {0:YYYY-MM-DD} ")%> </td> <td> <asp:lin Kbutton runat= "Server" id= "lbtupdate" commandargument= ' <%# DataBinder.Eval (Container.DataItem, "UserId")%> ' Commandname="Update" text= "Updates" ></asp:LinkButton> <asp:linkbutton runat= "Server" id= "Lbtcancel" commandargument= ' <%# DataBinder.Eval (Container.DataItem, " UserId ")%> ' commandname=" Cancel "text=" Cancel "></asp:LinkButton> </td> ; </tr> </asp:Panel> </ItemTemplate> <FooterTemplate> </tbody ></table> </FooterTemplate> </asp:Repeater></form>
Download code example: Pagedemo.rar
Related documents: pagination implementation of the Repeater control repeater multi-line interval display separator method repeater nested using Repeater