How to edit and delete a repeater Control

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.