Editing Records with EditItemTemplate

Editing Records with EditItemTemplate

The EditItemTemplate in a ListView control is used to define the layout of the editing interface when a user wants to edit a record. The EditItemTemplate is displayed when the user clicks the “Edit” button that is automatically added by the ListView control.

Here’s an example of how to use the EditItemTemplate to edit data in a ListView control:

<asp:LinqDataSource ID=”myDataSource” runat=”server”

    ContextTypeName=”MyDataContext” TableName=”MyTable”>

</asp:LinqDataSource>

<asp:ListView ID=”myListView” runat=”server” DataSourceID=”myDataSource”>

    <LayoutTemplate>

        <ul>

            <asp:PlaceHolder ID=”itemPlaceholder” runat=”server” />

        </ul>

    </LayoutTemplate>

    <ItemTemplate>

        <li>

            <%# Eval(“ColumnName1”) %> – <%# Eval(“ColumnName2”) %>

            <asp:Button ID=”EditButton” runat=”server” CommandName=”Edit” Text=”Edit” />

        </li>

    </ItemTemplate>

    <EditItemTemplate>

        <li>

            <asp:TextBox ID=”TextBox1″ runat=”server” Text='<%# Bind(“ColumnName1”) %>’ />

            <asp:TextBox ID=”TextBox2″ runat=”server” Text='<%# Bind(“ColumnName2”) %>’ />

            <asp:Button ID=”UpdateButton” runat=”server” CommandName=”Update” Text=”Update” />

            <asp:Button ID=”CancelButton” runat=”server” CommandName=”Cancel” Text=”Cancel” />

        </li>

    </EditItemTemplate>

</asp:ListView>

In this example, we have a LinqDataSource control that is bound to the MyTable table in the MyDataContext data context, and a ListView control that is bound to the LinqDataSource control.

In the ItemTemplate, we display the values of the ColumnName1 and ColumnName2 fields for each item in the ListView control. We also add an “Edit” button to each item.

In the EditItemTemplate, we use the Bind method to bind the values of the ColumnName1 and ColumnName2 fields to TextBox controls. We also add “Update” and “Cancel” buttons to the editing interface.

When the user clicks the “Edit” button in an item, the EditItemTemplate is displayed for that item, allowing the user to edit the values of the fields. When the user clicks the “Update” button, the changes are saved to the database. If the user clicks the “Cancel” button, the editing interface is hidden and the original values are restored.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu