Introducing the FormView Control

Introducing the FormView Control

The FormView control in ASP.NET is used to display a single record at a time from a data source. It is similar to the DetailsView control, but offers more flexibility in terms of customizing the layout and appearance of the displayed data.

To use the FormView control, you need to perform the following steps:

Define the FormView control in your ASPX page. This can be done by adding the following code:

<asp:FormView ID=”FormView1″ runat=”server”>

    <!– Define the layout of the control here –>

</asp:FormView>

Set the data source of the FormView control. This can be done by setting the DataSource property of the control to an instance of a data source control. For example, you can use the SqlDataSource control to retrieve data from a SQL Server database:

<asp:FormView ID=”FormView1″ runat=”server” DataSourceID=”SqlDataSource1″>

    <!– Define the layout of the control here –>

</asp:FormView>

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”…”>

    <!– Define the SQL query or stored procedure to retrieve data here –>

</asp:SqlDataSource>

Define the layout of the FormView control. This can be done using templates for different modes of the control (such as Edit, Insert, Item, and Empty modes). For example, to display the data in the Item mode, you can use the following code:

<asp:FormView ID=”FormView1″ runat=”server” DataSourceID=”SqlDataSource1″>

    <ItemTemplate>

        <div>

            <span>First Name:</span>

            <asp:Label ID=”FirstNameLabel” runat=”server” Text='<%# Eval(“FirstName”) %>’ />

        </div>

        <div>

            <span>Last Name:</span>

            <asp:Label ID=”LastNameLabel” runat=”server” Text='<%# Eval(“LastName”) %>’ />

        </div>

        <!– Add more fields here as needed –>

    </ItemTemplate>

</asp:FormView>

This code defines an ItemTemplate that displays the “First Name” and “Last Name” fields of the data source using Label controls. The Eval method is used to bind the value of the data field to the Text property of the Label control.

Optionally, you can add support for editing, inserting, and deleting data by defining templates for the Edit, Insert, and Empty modes. For example, to add support for editing the data, you can use the following code:

<asp:FormView ID=”FormView1″ runat=”server” DataSourceID=”SqlDataSource1″>

    <ItemTemplate>

        <!– Define the layout for displaying the data in the Item mode –>

    </ItemTemplate>

    <EditItemTemplate>

        <div>

            <span>First Name:</span>

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

        </div>

        <div>

            <span>Last Name:</span>

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

        </div>

        <!– Add more fields here as needed –>

    </EditItemTemplate>

    <InsertItemTemplate>

        <!– Define the layout for inserting new data here –>

    </InsertItemTemplate>

    <

Apply for ASP.NET Certification Now!!

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

Back to Tutorial

Ternary Operator in PHP
Loop constructs in PHP

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?