FormView Control in ASP.Net: Part 3

Introduction

In Part 2 of this article series we have discussed how to use Paging with FormView Control. Now in this article we will discuss how to Edit Data with FormView Control.

Edit Data with FormView Control

We can edit a database record with the FormView control. For example, we can use the technique given below to edit any of the records in the BOOK_LIST database table.

FormViw3.gif

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">

    <style type="text/css">
        html
    {
        background-color:silver;
    }
    #content
    {
        margin:auto;
        width:600px;
        padding:10px;
        background-color:white;
        font:14px Georgia,Serif;
    }
    a
    {
        color:blue;
    }
    </style>

    <title></title>
</head>
<
body>
    <form id="form1" runat="server">

    <div id="content">

    <asp:FormView
        id="frmBooks"
        DataSourceID="SqlDataSource1"
        DataKeyNames="ID"
        AllowPaging="true"
        Runat="server">
        <ItemTemplate>
        <b><u><%# Eval("TITLE")%></u></b>
        <br />
        <b>Serial Number:</b>
        <%# Eval("ID") %>
        <br />
        <b>Author Name:</b>
        <%# Eval("AUTHOR")%>
        <br />
        <b>Price:</b>
        <%# Eval("PRICE") %>
        <hr />
        <asp:LinkButton
            id="lnkEdit"
            Text="Edit Book"
            CommandName="Edit"
            Runat="server" />
        </ItemTemplate>
        <EditItemTemplate>
        <asp:Label
            id="lblTitle"
            Text="Book Title:"
            AssociatedControlID="txtTitle"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtTitle"
            Text='<%# Bind("TITLE") %>'
            Runat="server" />
        <br /><br />
        <asp:Label
            id="lblAuthor"
            Text="Author:"
            AssociatedControlID="txtAuthor"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtAuthor"
            Text='<%# Bind("AUTHOR") %>'
            Runat="server" />
        <br /><br />
        <asp:Label
            id="lblPrice"
            Text="Price:"
            AssociatedControlID="txtPrice"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtPrice"
            Text='<%# Bind("PRICE") %>'
            Runat="server" />
        <br /><br />
        <asp:LinkButton
            id="lnkUpdate"
            Text="Update Book"
            CommandName="Update"
            Runat="server" />
        <asp:LinkButton
            id="lnkCancel"
            Text="Cancel Update"
            CommandName="Cancel"
            Runat="server" />
        </EditItemTemplate>
    </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [AUTHOR], [PRICE] FROM [BOOK_LIST]"
            UpdateCommand="UPDATE BOOK_LIST SET ID =@ID, TITLE =@ID, AUTHOR =@AUTHOR, PRICE =@PRICE WHERE ID=@ID">
        </asp:SqlDataSource>

    </div>
    </form>
</body>
</
html>

Notice that several things about the FormView control in above example.

First, notice that the FormView control includes a DataKeyNames property that contains the name of the primary key from the data source. We need to specify a primary key when editing records.

Next, notice that the FormView control's ItemTemplate includes a LinkButton that looks like this:

<asp:LinkButton
            id="lnkEdit"
            Text="Edit Book"
            CommandName="Edit"
            Runat="server" />

This LinkButton includes a CommandName property with the value Edit. Clicking the link switches the FormView control into Edit mode. We could use any other control here that supports the CommandName property such as a Button or ImageButton control.

Next, notice that the FormView control includes an EditItemTemplate. This template contains the form for editing the record. Each form field uses a two-way databinding expression. For example, the form field for editing the book title looks like this:

<asp:TextBox
            id="txtTitle"
            Text='<%# Bind("TITLE") %>'
            Runat="server" />

The Bind("TITLE") method binds the TITLE column to the Text property of the TextBox control.

Finally, notice that the EditItemTemplate includes both a LinkButton for updating the database record and a LinkButton for canceling the update. The LinkButton for updating the record looks like this:

<asp:LinkButton
            id="lnkUpdate"
            Text="Update Book"
            CommandName="Update"
            Runat="server" />

This LinkButton includes a CommandName property, which has the value Update. When we click this LinkButton, the SQL statement represented by the SqlDataSource control's UpdateCommand is executed.

Note: Continue in Next Part.

HAVE A GREAT CODING!


Similar Articles