Data-bound controls in ASP.NET Web Forms are powerful components that allow developers to display and manipulate data from various data sources, such as databases, XML files, or collections. These controls automatically handle data binding, making it easier to create dynamic web applications. Below, we will explore how to use some common data-bound controls, including GridView, ListView, and DropDownList.

1. Setting Up the Data Source

Before using data-bound controls, you need to set up a data source. For this example, we will use a simple in-memory list of objects. In a real application, this data would typically come from a database.


public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
}

public List<Product> GetProducts()
{
return new List<Product>
{
new Product { ProductID = 1, ProductName = "Product A", Price = 10.00M },
new Product { ProductID = 2, ProductName = "Product B", Price = 20.00M },
new Product { ProductID = 3, ProductName = "Product C", Price = 30.00M }
};
}

2. Using GridView Control

The GridView control is one of the most commonly used data-bound controls. It displays data in a tabular format and supports features like sorting, paging, and editing.


<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvProducts_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ID" />
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Price" HeaderText="Price" />
</Columns>
</asp:GridView>

Code-behind to bind data:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvProducts.DataSource = GetProducts();
gvProducts.DataBind();
}
}

3. Using DropDownList Control

The DropDownList control allows users to select a single item from a list. It can be easily populated with data from a data source.


<asp:DropDownList ID="ddlProducts" runat="server" DataTextField="ProductName" DataValueField="ProductID"></asp:DropDownList>

Code-behind to bind data:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlProducts.DataSource = GetProducts();
ddlProducts.DataBind();
}
}

4. Using ListView Control

The ListView control provides a flexible way to display data using templates. It allows for more customization compared to GridView.


<asp:ListView ID="lvProducts" runat="server">
<ItemTemplate>
<div>
<h3><%= Eval("ProductName") %></h3>
<p>Price: <%= Eval("Price") %></p>
</div>
</ItemTemplate>
</asp:ListView>

Code-behind to bind data:


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvProducts.DataSource = GetProducts();
lvProducts.DataBind();
}
}

5. Conclusion

Data-bound controls in ASP.NET Web Forms simplify the process of displaying and manipulating data. They provide a straightforward way to connect to various data sources and present data in a user-friendly manner. By using controls like GridView, DropDownList, and ListView, developers can create dynamic and interactive web applications with minimal effort. Understanding how to effectively use these controls is essential for building robust ASP.NET applications that require data interaction.