Three methods for implementing the "delete" function in datalist

Source: Internet
Author: User

In the aspx fileCode:

<Asp: datalist id = "lbdatalist" runat = "server" repeatcolumns = "2" repeatdirection = "horizontal"
Skinid = "foo" Onitemcommand = "lbdatalist_itemcommand"> // The red part is effective in mojoportal.
<Itemtemplate>
<Table>
<Tr>
<TD>
<% # Eval ("lblc") %>
</TD>
<TD>
<Asp: linkbutton id = "datalist_delete" runat = "server" commandname = "delete" causesvalidation = "false"
TEXT = "delete" cssclass = "delete-link" commandargument = '<% # databinder. Eval

(Container. dataitem, "ID") %> '> </ASP: linkbutton>
</TD>
</Tr>
</Table>
</Itemtemplate>
</ASP: datalist>

In the Aspx. CS file, the "delete" feature of datalist is as follows:

// Of course, only one of the following three methods can be used to implement "delete !!

// method 1 of the "delete" function in datalist:
// In the "design" Mode on the page, select datalist, double-click the blank area on the right of "property" -- "Event"
// to automatically generate an empty method, then implement the deletion function
// (a sentence is automatically generated in the label:
// ondeletecommand = "lbdatalist_deletecommand")
protected void lbdatalist_deletecommand (Object source, datalistcommandeventargs e)
{< br> decimal id = convert. todecimal (E. commandargument); // get the ID to be deleted? Is the row ID of datalist or the data ID?
************ Add code here to "delete" ***********

// Bind again
* ********** Add code here and bind it again to see the effect after deletion **********

}

// Method 2:
// In the "Source" mode of the page, add the following command in <asp: linkbutton> </ASP: linkbutton>:
// Oncommand = "delete" (note: the two "delete" must be the same, and C # Is case sensitive)
// Add the following method to the CS code:
Protected void Delete (Object sender, commandeventargs E)
{

Decimal id = convert. todecimal (E. commandargument); // get the ID to be deleted? Is the row ID of datalist or the data ID?
* ********** Add code here to "delete "**********

// Bind again
* ********** Add code here and bind it again to see the effect after deletion **********

}

// method 3:
// In the "Source" mode of the page, in Add the following command:
// onitemcommand = "lbdatalist_itemcommand"
// (Note: Add oncommand = "delete" command)
// Add the following method to the CS code:
protected void lbdatalist_itemcommand (Object source, datalistcommandeventargs E)
{< br> decimal id = convert. todecimal (E. commandargument);
************ Add code here, to obtain the object to be deleted **********

If (E. commandname = "delete ")
{
* ********** Add code here to achieve "delete "**********}

// Bind again
* ********** Add code here and bind it again to see the effect after deletion **********

}

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.