In the Repeater control, how does one implement the same edit, update, and delete functions as the GridView control?
Copy codeThe Code is 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%">
Email
</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>