FormView
is a data-bound control. It is nothing but a templated version of DetailsView
control. The difference between DetailsView and FormView is, here user need to
define the rendering template for each item.
Description:
In previous articles I have explained Error:
ASP.NET:
How to Get Public IP address in ASP.NET using C# & VB.NET? And Custom
Validator: Server Side validation using Custom Validator in ASP.NET (C# &
Vb.NET) And ASP.NET:
Bind or Populate Form View Data control with data from SQL database using c#
& vb.net .
In
this article I wll explain how to bind Form view Data control to databse using
SQl query.
Demo:
Click on Image to Enlarge view
Steps
to display Data in FormView:
1.
Create webpage in your
application and add Form view control.
2.
Now Customize Form
View ItemTemplate to bind column name
as given in table in sql.
3.
Write code in code
behind file to bind Form view to database.
Implementation:
Create table in database:
Create Table
“Student_Info” in Database “Blog” and
Enter some data to display.
Column Name
|
DataType
|
Student_Id
|
Int
|
Student_name
|
Varchar(100)
|
Age
|
Int
|
Class
|
Varchar(100)
|
Now
design your web page:
Place
FormView control on webpage. Customize it as given below:
<fieldset style="width:320px"><legend>Display
Data in Form View Control</legend>
<br />
<asp:FormView ID="FormView1" runat="server" ForeColor="#333333" Width="300px"
DataKeyNames="Student_id" AllowPaging="true" OnPageIndexChanging="FormView1_PageIndexChanging">
<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" style="width:100%">
<tr>
<td>Student
id</td>
<td><%# Eval("Student_id") %></td>
</tr>
<tr>
<td>Name</td>
<td><%# Eval("Student_Name") %></td>
</tr>
<tr>
<td>Age</td>
<td><%# Eval("Age")
%></td>
</tr>
<tr>
<td>Class</td>
<td><%# Eval("Class")
%></td>
</tr>
</table>
</ItemTemplate>
</asp:FormView><br />
</fieldset>
|
Asp.NET Code:
Now write following code to Bind Form View Data Control.
In
C#:
Add
Following Namespaces:
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
|
Write
following code:
public partial class Formview
: System.Web.UI.Page
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_FormView();
}
}
public void
Bind_FormView()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand
cmd = new SqlCommand("select * from student_info", con);
SqlDataAdapter
adpData = new SqlDataAdapter(cmd);
DataTable
dt = new DataTable();
adpData.Fill(dt);
FormView1.DataSource = dt;
FormView1.DataBind();
con.Close();
}
protected void
FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex
= e.NewPageIndex;
Bind_FormView();
}
}
|
In Vb.Net:
Add Following Namespaces:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Write following code:
Partial Public Class Formview
Inherits
System.Web.UI.Page
Private
con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
Protected Sub
Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not
IsPostBack Then
Bind_FormView()
End If
End Sub
Public Sub
Bind_FormView()
If con.State = ConnectionState.Closed
Then
con.Open()
End If
Dim
cmd As New SqlCommand("select * from student_info", con)
Dim
adpData As New SqlDataAdapter(cmd)
Dim
dt As New DataTable()
adpData.Fill(dt)
FormView1.DataSource = dt
FormView1.DataBind()
con.Close()
End Sub
Protected Sub
FormView1_PageIndexChanging(sender As Object, e As FormViewPageEventArgs) Handles FormView1.PageIndexChanging
FormView1.PageIndex = e.NewPageIndex
Bind_FormView()
End Sub
End Class
|