Insert, edit, and delete functions of the ListView Control (Part Two)

Source: Internet
Author: User

This series of articles will demonstrate how to use the ASP. NET 3.5 ListView Control for basic Insert, edit, and delete operations with a simple example, combined with my own usage of the ListView.

System Requirements:
    • Windows XP SP2 or higher
    • VS2008 Beta 2 or Visual Web Developer Express Edition Beta 2

In the first article in this series, we describe how to implement the Insert feature of the ListView. This article describes how to implement the edit function.

Implementing the Edit function

Adding the Edit function to the ListView is very similar to the Insert method. We need to add a edititemtemplate with two textbox inside to receive the user input for name and value, and a Save button. The commandname of this Save button is set to update:

<EditItemTemplate> <li>name: <asp:textbox runat="Server"Id="Name"text='<%# Bind ("Name")%>'/>Value:<asp:textbox runat="Server"Id="Value"text='<%# Bind ("Value")%>'/> <br/> <asp:button runat="Server"Id="Update"text="Save"Commandname="Update"/> </li></EditItemTemplate>

Also, add an Edit button to the ItemTemplate, and its commandname is set to edit:

<ItemTemplate>    <li><%# eval ("Name")%> (#<%# eval (" Value ") %>) <br/>    <asp:button runat="server" id="Edit " Commandname="edit " text="edit" />    < /LI><BR/></itemtemplate>

Then add the ItemUpdating event handling method to the ListView. In this method, if we get the currently modified data object? There are three methods I've used:

    • Add the CommandArgument property to the Save button whose value is set to the value of the current data object, but the CommandArgument value cannot be obtained directly in the itemupdating, so the processing code for this method Should be moved to the handling of the ItemCommand event;
    • In EditItemTemplate, place a literal whose Text property is set to the value of the data object and hide the literal. In ItemUpdating, the literal is obtained by using the FindControl method for the current item, and the value is obtained.
    • Use the DataKeyNames property of the ListView;

We will use the 3rd method. Add the DataKeyNames property to the ListView and set its value to value. Thus, when the ListView is populated with data, the DataKeys property of the ListView is automatically populated with the value of each data object.

In the ItemUpdating processing method, we take the itemindex value of the event parameter Listviewupdateeventargs as the subscript, get the value of the currently edited data object from the DataKeys of the ListView, and then save the operation , and finally, set the Editindex of the ListView to 1 to exit the edit state:

protected voidListview1_itemupdating (Objectsender, Listviewupdateeventargs e) {    intValue =int. Parse (Listview1.datakeys[e.itemindex].    Value.tostring ()); List<DataEntry> data =Data; DataEntry entry= data. Single (d = D.value = =value); Entry. Name= (Listview1.items[e.itemindex]. FindControl ("Name") asTextBox).    Text; Entry. Value=int. Parse ((Listview1.items[e.itemindex). FindControl ("Value") asTextBox).    Text); Data=data; Listview1.editindex= -1; Binddata ();}

, add a processing method for the Itemediting event of the ListView, and set the Editindex property of the ListView to enter the edit state. The Editindex value can be obtained from the Neweditindex of the event parameter Listviewediteventargs:

protected void Listview1_itemediting (object  sender, Listviewediteventargs e) {    = e.neweditindex;    Binddata ();}

Run the sample website and test the edit function, seemingly normal. Map:) Note: We assume that the value of the data object is unique (equivalent to the primary), and in order not to introduce too much detail, add, modify data without checking for uniqueness of the value.

~ @ashchan

Insert, edit, and delete functions of the ListView Control (Part Two)

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.