I have been doing asp.net for six years, and the Repeater control has been used for countless times, but each time it is only displayed.
Today, we need to edit and delete the feature. google just a bit. Add RepeaterOnItemDataBoundEvent.
Onitemcommand = "repeaterincluitemcommand"
1. The deletion function is similar to that of the GridView control. A Button is displayed at the front end and
Commandargument = '<% # eval ("ID") %> 'commandname = "delete"
Note that the CommandArgument value must be enclosed in single quotes; otherwise, an error is returned.
Background code
Protected void repeaterincluitemcommand (Object source, repeatercommandeventargs E)
{
If (E. commandname = "delete ")
{
This. page. clientscript. registerstartupscript (this. page. getType (), "key", "alert ('delete ID:" + E. commandargument + "');", true );
Int id = convert. toint32 (E. commandargument );
//... Delete...
}
Bindgrid ();
}
2. there are two ways to edit an operation. The first is to put two panels in the <itemtemplate> Of the repeater at the front end, put the displayed and edited content respectively, and use itemdatabound to control which one to display, the button is similar to the delete button.
OnItemDataBound = "repeaterincluitemdatabound" protected void repeaterincluitemdatabound (object sender, System. Web. UI. WebControls. RepeaterItemEventArgs e)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
Datarowview rowv = (datarowview) E. Item. dataitem;
String userid = rowv ["userid"]. tostring ();
If (userid! = ID. tostring ())
{
(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;
}
}
}
In the ItemCommand method
View Code
Protected void repeaterincluitemcommand (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 ")
{
// Update.
String username = (TextBox) this. Repeater1.Items [e. Item. ItemIndex]. FindControl ("UserName"). Text. Trim ();
This. page. clientScript. registerStartupScript (this. page. getType (), "key", "alert ('Update ID:" + e. commandArgument + "; Page value:" + username + "');", true );
}
Else if (e. CommandName = "Delete ")
{
// Delete.
This. page. clientScript. registerStartupScript (this. page. getType (), "key", "alert ('delete ID:" + e. commandArgument + "');", true );
}
BindGrid ();
}
Another method of editing is to click the edit button to go to the new page to edit it. I personally think this form is better. I have read some forums or blogs and basically used this method.