Strong Typed FormView control


In this article we will strongly type the FormView control using Visual Studio 2012(.Net Framework 4.5). Using Strong typing helps in checking for errors while binding data when you write the code and makes it easy for development.

In this article we will strongly type the FormView control using Visual Studio 2012(.Net Framework 4.5). Using Strong typing helps in checking for errors while binding data when you write the code and makes it easy for development.
Step 1:
We are going to use EmployeeDB database which contains an Emp table with Employee Details as follows:
db
Step 2:
Create a New Web Application, Name:StrongDTypeDataBoundDemo, Right click the project and select Add New Item -> Data -> LinqToSql Classes. Name it as Emp.dbml
Step 3:
Now from the Server Explorer window expand the Database and then expand the Tables and then Drag and Drop the Emp table to the Emp.dbml designer window as shown below:
se
dbml
Step 4:
Now add a FormView control to the Default.aspx page as shown below:


<asp:FormView ID="FormView1" ItemType="StrongDTypeDataBoundDemo.Emp" runat="server">
<ItemTemplate>
<div>
Emp Id :
<asp:Label ID="Label3" runat="server" Text='<%#BindItem.EmpId %>'></asp:Label><br />
</div>
<div>
Emp Name :
<asp:Label ID="lblEmpName" runat="server" Text='<%#BindItem.EmpName %>'></asp:Label><br />
</div>

<div>
Salary :
<asp:Label ID="Label1" runat="server" Text='<%#BindItem.Salary %>'></asp:Label><br />
</div>

<div>
Hire Date :
<asp:Label ID="Label2" runat="server" Text='<%#Item.HireDate.ToShortDateString() %>'></asp:Label>
</div>
</ItemTemplate>
</asp:FormView>


The output of above code is as shown below:
output
Important points:
1.Here FormView is a Strongly-Typed data control because we are using the ItemType property in the FormView. we are setting it to "StrongDTypeDataBoundDemo.Emp" this means that the controls present in the FormView control will be data bound to the properties of the Emp class(i.e., Columns of the Emp table). In the above FormView control we are binding Emp details (EmpId,EmpName,Salary,HireDate) from the Emp class, Here we are setting ItemType="StrongDTypeDataBoundDemo.Emp" Emp is the LinqToSql entity class which holds the Emp table information.

2.Then we are using BindItem to display the database table column values using the BindItem.ColumnName naming convention. We can use either BindItem or Item to access the properties of the Emp class.

3. We want to format the HireDate column to display only the date part without the time for this reason we are using Item.HireDate.ToShortDateString() to display only the date.

4.If we type an invalid column name in the FormView child controls to bind the invalid column. It gives an error as shown below and we can correct it to EmpName column.


Attachments

Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: