How to Leverage the Strongly Typed Data Control in Asp.net 4.5 Web Forms

0
272
views
How to Leverage the Strongly Typed Data Control in Asp.net 4.5 Web Forms

Hire asp.net developers in India who have knowledge of the strongly type Binding feature in asp.net 4.5. In this story, you will learn the use of the latest feature offered by asp.net 4.5 to development community.







Strongly type binding is one of the new feature in Asp.net 4.5.

Normally we use late binding techniques in Asp.Net Data Controls like GridView and ListView. When we use the late binding, binding will be happened during the runtime. In this case we use “Eval(expression)” for achieving the runtime binding. In this case, data type will not be specified.So it will throw an exception when the expression is not evaluated properly.

In Asp.Net 4.5 Strongly Type Binding is introduced to bind the data to the display control with strong types. So if there is an exception occurs, it will evaluate and eliminate the errors.

In this article I am going to explain both earlier binding technique without strong type and Strongly type binding.

We need to do the following steps for demonstrating the Data Binding in Asp.net

  • Create a new class (User defined Type)
  • Create the Generic list type method for Binding the data
  • Create Aspx Web page for display the Grid Control and Display the records.




First, I like to show the earlier binding Technical.

Create a Part Class

Public class Part
{
Public int PartNum { get; set; }
Public string PartName { get; set; }
Public string PartType { get; set; }
Public decimal Qty { get; set; }
Public decimal Price { get; set; }
}

Create a method for BindGrid which will return the generic list type Part.

Public IList<Part> BindGrid()
{
Return new List<Part>
{
new Part{PartNum=1,PartName=”Part1″,PartType=”M”,Qty=10.00M,Price=1.1M},
new Part{PartNum=2,PartName=”Part2″,PartType=”M”,Qty=10.00M,Price=1.1M},
new Part{PartNum=3,PartName=”Part3″,PartType=”M”,Qty=10.00M,Price=1.1M},
new Part{PartNum=4,PartName=”Part4″,PartType=”M”,Qty=10.00M,Price=1.1M},
};
}

We will useEval(expression) for binding the data in List View Control

<asp:ListView ID=”ShowPart” runat=”server”>
<LayoutTemplate>
<table id=”h1″ runat=”server”>
<tr id=”hr1″ runat=”server”>
<td id=”hd1″ runat=”server”>
PartNum
</td>
<td id=”hd2″ runat=”server”>
PartName
</td>
<td id=”hd2″ runat=”server”>
PartType
</td>
<td id=”hd2″ runat=”server”>
Qty
</td>
<td id=”hd2″ runat=”server”>
Price
</td>
</tr>
<tr id=”PartItem” runat=”server”>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID=”Label1″ runat=”server” Text='<%# Eval(“PartNum”)%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Eval(“PartType”)%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Eval(“Qty”)%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Eval(“Price”)%>’>
</asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

protected void Page_Load(object sender, EventArgs e)
{
ShowPart.DataSource = BindGrid();
ShowPart.DataBind();
}



In this section, I am going to explain how we can bind the above list view using Strongly Typed data. In Asp.Net 4.5 data controls, new binding property is introduced called ItemType. In this ItemType we can specify the Column Name. like <%#Item.ColumnName#%>.

<asp:ListView ID=”ShowPart” runat=”server” ItemType=”Part”>
<LayoutTemplate>
<table id=”h1″ runat=”server”>
<tr id=”hr1″ runat=”server”>
<td id=”hd1″ runat=”server”>
PartNum
</td>
<td id=”hd2″ runat=”server”>
PartName
</td>
<td id=”hd2″ runat=”server”>
PartType
</td>
<td id=”hd2″ runat=”server”>
Qty
</td>
<td id=”hd2″ runat=”server”>
Price
</td>

</tr>
<tr id=”PartItem” runat=”server”>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID=”Label1″ runat=”server” Text='<%# Item.PartNum%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.PartType%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.Qty%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.Price%>’>
</asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

In the above code I have used the Item Type. which is used to bind the data with respectivedata Type to the control. Here we have used Part as a custom data Type. For getting the values from the Part class We need to use the Keyword “Item”, Visual Studio intelligence will automatically show the property values like “Item.PartNum”. This helps us to bind the strongly typed data to the controls.

In the same way, we can use the SelectMethod property, which is used to select the data from the data source.To fetch the data, we need to write the method called “Showpartdata” in Code behind and it is called in SelectMethod property.



<asp:ListView ID=”ShowPart” runat=”server” ItemType=”Part”SelectMethod=”ShowPartdata” >
<LayoutTemplate>
<table id=”h1″ runat=”server”>
<tr id=”hr1″ runat=”server”>
<td id=”hd1″ runat=”server”>
PartNum
</td>
<td id=”hd2″ runat=”server”>
PartName
</td>
<td id=”hd2″ runat=”server”>
PartType
</td>
<td id=”hd2″ runat=”server”>
Qty
</td>
<td id=”hd2″ runat=”server”>
Price
</td>
</tr>
<tr id=”PartItem” runat=”server”>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID=”Label1″ runat=”server” Text='<%# Item.PartNum%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.PartType%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.Qty%>’>
</asp:Label>
</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text='<%# Item.Price%>’>
</asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>

Make sure you hire asp.net developers in India understand the use of the latest feature of asp.net 4.5. In this article, experts have explained the process of binding the List view control in both Strong typed data binding and conventional binding. For queries related to the subject, make comments below.

Conclusion




In this article I have explained how we can bind the List view control in both Conventional binding and strongly typed data binding. In Strongly Typed binding we can directly binding property with respective type, so it will not trigger any type casting issue.

Hope this article is useful in Understanding the concept of typed bindings.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × 1 =