FormView Control in ASP.Net: Part 4

Introduction

In Part 3 of this article series we have discussed how to Edit Data with FormView Control. Now in this article we will discuss how Insert New Record with FormView Control.

Inserting New Record with FormView Control

We can use the FormView control to insert new records into a database table. For example, the page given below enables us to insert a new book record into the BOOK_LIST database table.

FormView4.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"
        AllowPaging="true"
        DataKeyNames="ID"
        Runat="server" >
        <ItemTemplate>
        <h1><%# Eval("TITLE")%></h1>
        <b>Author:</b>
        <%# Eval("AUTHOR")%>
        <br />
        <b>Price:</b>
        <%# Eval("PRICE")%>
        <hr />
        <asp:LinkButton
            id="lnkNew"
            Text="New Book"
            CommandName="New"
            Runat="server" />
        </ItemTemplate>
        <InsertItemTemplate>
        <asp:Label
            id="lblTitle"
            Text="Book Name:"
            AssociatedControlID="txtTitle"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtTitle"
            Text='<%# Bind("TITLE") %>'
            Runat="server" />
        <br />
        <asp:Label
            id="lblId"
            Text="ID:"
            AssociatedControlID="txtID"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtID"
            Text='<%# Bind("ID") %>'
            Runat="server" />
        <br />
        <asp:Label
            id="lblAuthor"
            Text="Author Name:"
            AssociatedControlID="txtAuthor"
            Runat="server" />
        <br />
        <asp:TextBox
            id="txtAuthor"
            Text='<%# Bind("AUTHOR") %>'
            Runat="server" />
            <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="lnkInsert"
            Text="Insert New Book"
            CommandName="Insert"
            Runat="server" />
        <asp:LinkButton
            id="lnkCancel"
            Text="Cancel Insert"
            CommandName="Cancel"
            Runat="server" />
        </InsertItemTemplate>
    </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
            ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
            SelectCommand="SELECT [ID], [TITLE], [AUTHOR], [PRICE] FROM [BOOK_LIST]"
            InsertCommand="INSERT INTO BOOK_LIST(ID, TITLE, AUTHOR, PRICE) VALUES (@ID, @TITLE, @AUTHOR, @PRICE)">
        </asp:SqlDataSource>

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

We should notice several things in above coding. First, notice that the ItemTemplate includes a LinkButton control that looks like this:

<asp:LinkButton
            id="lnkInsert"
            Text="Insert New Book"
            CommandName="Insert"
            Runat="server" />

When we click this LinkButton control, the FormView switches into Insert mode and displays the contents of the InsertTemplate. Notice that the CommandName property has the value New.

Notice that this LinkButton control includes a CommandName property that has the value Insert. When we click the LinkButton, the SQL command represented by the SqlDataSource control's InsertCommand is executed. We can also place the FormView control into Insert mode by default by assigning the value Insert to the control's DefaultMode property.

Note: Continue in Next Part.

HAVE A GREAT CODING!


Similar Articles