How do I implement edit, update, and delete functions like the GridView control in a Repeater control?
Copy Code code as follows:
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 = "Edit")
{
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");
}
<</code>form id= "Form1" runat= "Server" >
<</code>asp:repeater id= "Rptuser" runat= "Server" onitemcommand= "Rptuser_itemcommand"
onitemdatabound= "Rptuser_itemdatabound" >
<</CODE>HeaderTemplate>
<</code>table width= "960" align= "Center" cellpadding= "3" cellspacing= "1" style= "Background-color: #ccc;" >
<</code>thead style= "Background-color: #eee;" >
<</CODE>tr>
<</code>th width= "10%" >
User ID
</</CODE>th>
<</CODE>th>
User name
</</CODE>th>
<</code>th width= "22%" >
Mail
</</CODE>th>
<</code>th width= "20%" >
Qq
</</CODE>th>
<</code>th width= "15%" >
Registration time
</</CODE>th>
<</code>th width= "12%" >
Operation
</</CODE>th>
</</CODE>tr>
</</CODE>thead>
<</code>tbody style= "Background-color: #fff;" >
</</CODE>HeaderTemplate>
<</CODE>ItemTemplate>
<</code>asp:panel id= "Plitem" runat= "Server" >
<</code>tr style= "Text-align:center;" >
<</CODE>td>