Friday 23 November 2012

FormView Control in Asp.Net 4.0 using C#.


Introduction:

FormView control display only single record in a table at a time retrieved from datasource. Template are used to display and edit the data value in a FormView control. When multiple records are retieved from the datasorce then paging is usd to view all the records. It supports basic functions like select ,edit and deleting data records.
Drag and drop a FormView Control on your web page. Click on fromview control , you will see at the top right corner there is rectangular box , click on this box -> choose data source  from the drop down list -> select new data source . See the figure given below..


After this choose SqlDataSource as shown in the figure.

After it will open Configure data source window. Select your connection from the drop down list, if connection is not available then click on new connection option. To countinue ,click on the next button. In the next screen , pass the sql query to the ‘Configure the select statement ‘ screen as shown in the figure below.


Click Finish Button. This will automatically add the sqlDataSuorce control to the web page.
If you want show all data in Form View then Check the EnablePaging CheckBox as shown in the figure which is given below.


FormView.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormView.aspx.cs" Inherits="FormView" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:FormView ID="FormView1" runat="server" AllowPaging="True"
            DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
            <EditItemTemplate>
                EmployeeID:
                <asp:Label ID="EmployeeIDLabel1" runat="server"
                    Text='<%# Eval("EmployeeID") %>' />
                <br />
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                BirthDate:
                <asp:TextBox ID="BirthDateTextBox" runat="server"
                    Text='<%# Bind("BirthDate") %>' />
                <br />
                MaritalStatus:
                <asp:TextBox ID="MaritalStatusTextBox" runat="server"
                    Text='<%# Bind("MaritalStatus") %>' />
                <br />
                Gender:
                <asp:TextBox ID="GenderTextBox" runat="server" Text='<%# Bind("Gender") %>' />
                <br />
                HireDate:
                <asp:TextBox ID="HireDateTextBox" runat="server"
                    Text='<%# Bind("HireDate") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                    CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>
                Name:
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                BirthDate:
                <asp:TextBox ID="BirthDateTextBox" runat="server"
                    Text='<%# Bind("BirthDate") %>' />
                <br />
                MaritalStatus:
                <asp:TextBox ID="MaritalStatusTextBox" runat="server"
                    Text='<%# Bind("MaritalStatus") %>' />
                <br />
                Gender:
                <asp:TextBox ID="GenderTextBox" runat="server" Text='<%# Bind("Gender") %>' />
                <br />
                HireDate:
                <asp:TextBox ID="HireDateTextBox" runat="server"
                    Text='<%# Bind("HireDate") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                    CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <ItemTemplate>
                EmployeeID:
                <asp:Label ID="EmployeeIDLabel" runat="server"
                    Text='<%# Eval("EmployeeID") %>' />
                <br />
                Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Title:
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                BirthDate:
                <asp:Label ID="BirthDateLabel" runat="server" Text='<%# Bind("BirthDate") %>' />
                <br />
                MaritalStatus:
                <asp:Label ID="MaritalStatusLabel" runat="server"
                    Text='<%# Bind("MaritalStatus") %>' />
                <br />
                Gender:
                <asp:Label ID="GenderLabel" runat="server" Text='<%# Bind("Gender") %>' />
                <br />
                HireDate:
                <asp:Label ID="HireDateLabel" runat="server" Text='<%# Bind("HireDate") %>' />
                <br />

            </ItemTemplate>
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:modiConnectionString %>"
            SelectCommand="SELECT [EmployeeID], [Name], [Title], [BirthDate], [MaritalStatus], [Gender], [HireDate] FROM [Employee]">
        </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

Press F5 button and see the output. 

No comments:

Post a Comment

Note: only a member of this blog may post a comment.