SoFunction
Updated on 2025-03-10

Repeater control implements example code for editing, updating, deleting and other operations


protected void Page_Load(object sender, EventArgs e)
{
if (!)
{
BindGrid();
}
}
private void BindGrid()
{
string strSQL = "SELECT * FROM [User]";
OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
();
OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
OleDbDataReader reader = ();
= reader;
();
}
protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ( == || == )
{
record = ();
int userId = (record["UserId"].ToString());
if (userId != id)
{
((Panel)("plItem")).Visible = true;
((Panel)("plEdit")).Visible = false;
}
else
{
((Panel)("plItem")).Visible = false;
((Panel)("plEdit")).Visible = true;
}
}
}
protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if ( == "Edit")
{
id = (());
}
else if ( == "Cancel")
{
id = -1;
}
else if ( == "Update")
{
string name = ((TextBox)[].FindControl("txtName")).();
string email = ((TextBox)[].FindControl("txtEmail")).();
string qq = ((TextBox)[].FindControl("txtQQ")).();
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);
("@Name", );
["@Name"].Value = name;
("@Email", );
["@Email"].Value = email;
("@QQ", );
["@QQ"].Value = qq;
("@UserId", );
["@UserId"].Value = (());
();
();
();
}
else if ( == "Delete")
{
string strSQL = "DELETE * FROM [User] WHERE UserId=@UserId";
OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
("@UserId", );
["@UserId"].Value = (());
();
();
();
}
BindGrid();
}
private string GetStrConnection()
{
return "Provider=.4.0;data source=" + ("~/Database/");
}

<</CODE>form 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>
username
</</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%">
operate
</</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>