ASP.NET FormView control by VB.NET

In this article we will learn how to use FormView control to display a single record from a datasource.
  • 5123
 

The FormView control is used to display a single record from a data source in a table. FormView control Display Data with the FormView Control. The FormView control is similar to the DetailsView. Rather than using field tags like <asp:BoundField>, the FormView specifies its data using template tags like <ItemTemplate>. Use the itemTemplate to display database records. The FormView control allows you to edit, delete, and insert records.

  1. Open Visual Studio.
  2. Add a webForm to your website, name it formView.aspx.

Now drag and drop a FormView control from the Toolbox on the form.

fv1.gif
 

Figure1

The ASP. NET code for the DataView control.

<form id="form1" runat="server">

   <div style="width: 262px">

          <asp:FormView ID="FormView1" runat="server" CellPadding="4"

           DataSourceID="SqlDataSource1" ForeColor="#333333" Height="137px"

           Width="380px" Caption="User Detail" CaptionAlign="Top">

           <EditRowStyle />

              <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

              <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

           <ItemTemplate>

                UserId:

               <asp:Label ID ="userid" runat ="server" Text ='<%# Bind("UserId") %>'></asp:Label>

               <br />

                Username:

               <asp:Label ID ="username" runat ="server" Text ='<%# Bind("Username") %>'></asp:Label>

               <br />

                Email:

                <asp:Label ID ="useremail" runat ="server" Text ='<%# Bind("Email") %>'></asp:Label>

               <br />

                Address:

                <asp:Label ID ="useraddress" runat ="server" Text ='<%# Bind("Address") %>'></asp:Label>

               <br />

           </ItemTemplate>

              <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

               <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />         

       </asp:FormView>

      

 

Now The following code snippet shows how to connect SQL database.

 

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

    Dim conn As New SqlConnection("Data Source=.;uid=sa;pwd=Password$2;database=master")

    Dim ad As New SqlDataAdapter()

    Dim cmd As New SqlCommand()

    Dim dataTable As DataTable

 

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandlesMe.Load

        dataTable = New DataTable()

        cmd.Connection = conn

        cmd.CommandText = "SELECT * FROM userinfo1"

        ad = New SqlDataAdapter(cmd)

        ad.Fill(dataTable)

        FormView1.DataSource = dataTable

        FormView1.DataBind()

    End Sub

End Class

 

Now run the application.


 

fv2.gif
 

 

Figure2

 

Paging, Border Style and caption.

 

The ASP. NET code for DataView control property.

 

<asp:FormView ID="FormView1" runat="server" CellPadding="4"

 ForeColor="#333333" Height="137px"

Width="380px" AllowPaging="True" BorderColor="#FF3399" BorderStyle="Solid"

Caption="User Detail" CaptionAlign="Top">

 

Now run the application again and test it.


 

fv3.gif
 

 

Figure3

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.