Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Advertisements


Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
asp:FormView control
FormView is a new data-bound control that is nothing but a templated version of DetailsView control.
 
FormView is a new data-bound control that is nothing but a templated version of DetailsView control. The major difference between DetailsView and FormView is, here user need to define the rendering template for each item.

Its properties like BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. are implemented through style properites of <tahle> tag.

Following are some important properties that are very useful.
Templates of the FormView Control
EditItemTemplate The template that is used when a record is being edited.
InsertItemTemplate The template that is used when a record is being created.
ItemTemplate The template that is used to render the record to display only.
Methods of the FormView Control
ChangeMode ReadOnly/Insert/Edit. Change the working mode of the control from the current to the defined FormViewMode type.
InsertItem Used to insert the record into database. This method must be called when the DetailsView control is in insert mode.
UpdateItem Used to update the current record into database. This method must be called when DetailsView control is in edit mode.
DeleteItem Used to delete the current record from database.
DEMO : FormView Show Source Code
Try Inserting Records into Database
AutoID
Name
Address
Phone
City
 
 
      
                    
 // FormView control ////////////////////////////////
 <asp:FormView ID="FormView1" runat="server" CellPadding="4" ForeColor="#333333"
     DataKeyNames="AutoID" DataSourceID="SqlDataSource1" AllowPaging="true">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <ItemTemplate>
            <table border="1">
                <tr>
                    <td>AutoID</td>
                    <td><%# Eval("AutoID") %></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><%# Eval("Name") %></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><%# Eval("Address") %></td>
                </tr>
                <tr>
                    <td>Phone</td>
                    <td><%# Eval("Phone") %></td>
                </tr>
                <tr>
                    <td>City</td>
                    <td><%# Eval("City") %></td>
                </tr>
                <tr>
                    <td> </td>
                    <td>
                        <asp:Button ID="btnEdit" runat="Server" CommandName="Edit" Text="Edit" />
                        <asp:Button ID="btnInsert" runat="Server" CommandName="New" Text="New" />
                        <asp:Button ID="btnDelete" runat="Server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure to Delete?');" />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <EditItemTemplate>
            <table border="1">
                <tr>
                    <td>AutoID</td>
                    <td><%# Eval("AutoID") %></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><asp:TextBox ID="TextBox1" runat="Server" Text='<%# Bind("Name")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><asp:TextBox ID="TextBox2" runat="Server" Text='<%# Bind("Address")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Phone</td>
                    <td><asp:TextBox ID="TextBox3" runat="Server" Text='<%# Bind("Phone")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>City</td>
                    <td><asp:TextBox ID="TextBox4" runat="Server" Text='<%# Bind("City")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td> </td>
                    <td>
                        <asp:Button ID="btnUpdate" runat="Server" CommandName="Update" Text="Update" />
                        <asp:Button ID="Button1" runat="Server" CommandName="Cancel" Text="Cancel" />
                    </td>
                </tr>
            </table>
        </EditItemTemplate>
        <InsertItemTemplate>
            <table border="1">
                <tr>
                    <td>AutoID</td>
                    <td><%# Eval("AutoID") %></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><asp:TextBox ID="TextBox1" runat="Server" Text='<%# Bind("Name")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Address</td>
                    <td><asp:TextBox ID="TextBox2" runat="Server" Text='<%# Bind("Address")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Phone</td>
                    <td><asp:TextBox ID="TextBox3" runat="Server" Text='<%# Bind("Phone")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td>City</td>
                    <td><asp:TextBox ID="TextBox4" runat="Server" Text='<%# Bind("City")%>'></asp:TextBox></td>
                </tr>
                <tr>
                    <td> </td>
                    <td>
                        <asp:Button ID="btnSave" runat="Server" CommandName="insert" Text="Insert" />
                        <asp:Button ID="Button1" runat="Server" CommandName="Cancel" Text="Cancel" />
                    </td>
                </tr>
            </table>
        </InsertItemTemplate>
    </asp:FormView>
   
   
   // SqlDataSource Control ////////////////////////////////
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:ConnStr %>'
     SelectCommand="Select * FROM SampleForTutorials ORDER BY [Name]"
     DeleteCommand="Delete FROM SampleForTutorials WHERE AutoID = @AutoID"
     UpdateCommand="UPDATE SampleForTutorials SET Name = @Name, Address = @Address, Phone = @Phone, City = @City WHERE AutoID = @AutoID"
     InsertCommand="INSERT INTO SampleForTutorials (Name, Address, Phone, City) VALUES (@Name, @Address, @Phone, @City)">
        <DeleteParameters>
            <asp:Parameter Name="AutoID" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="AutoID" Type="Int32" />
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone"  Type="string" Size="50" />
            <asp:Parameter Name="City" Type="string" Size="20" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="string" Size="50" />
            <asp:Parameter Name="Address" Type="string" Size="200" />
            <asp:Parameter Name="Phone" Type="string" Size="50" />
            <asp:Parameter Name="City" Type="string" Size="20" />
            <asp:Parameter Name="AutoID" Type="Int32" />
        </InsertParameters>
     </asp:SqlDataSource>