Introduction
In ASP.NET WebForms, you often need to handle multiple values — like product names, user IDs, or form inputs — together.
That’s where Arrays and Lists come in.
In this blog, we’ll understand both with real ASP.NET C# examples, such as displaying product names in a GridView.
What is an Array in C#?
An Array is a collection that stores multiple values of the same type in a fixed size.
Example
string[] products = { "Laptop", "Mouse", "Keyboard", "Monitor" };
You can access values using index positions:
Response.Write(products[0]); // Output: Laptop
Example 1: Display Array Data in ASP.NET WebForm
ASPX Page (ArrayExample.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ArrayExample.aspx.cs" Inherits="WebFormsDemo.ArrayExample" %>
<!DOCTYPE html>
<html>
<head>
<title>Array Example in ASP.NET</title>
</head>
<body>
<h2>Array Example: Product List</h2>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
</body>
</html>
Code Behind (ArrayExample.aspx.cs)
using System;
using System.Data;
namespace WebFormsDemo
{
public partial class ArrayExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Step 1: Create an array of products
string[] products = { "Laptop", "Mouse", "Keyboard", "Monitor", "Printer" };
// Step 2: Convert array to DataTable to display in GridView
DataTable dt = new DataTable();
dt.Columns.Add("Product Name");
foreach (string item in products)
{
dt.Rows.Add(item);
}
// Step 3: Bind data to GridView
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Output
| Product Name |
|---|
| Laptop |
| Mouse |
| Keyboard |
| Monitor |
| Printer |
What is a List in C#?
A List is a generic collection that allows dynamic resizing — you can add, remove, or modify elements easily.
Example
List<string> employees = new List<string>();
employees.Add("John");
employees.Add("Priya");
Example 2: Display List Data in ASP.NET WebForm
ASPX Page (ListExample.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListExample.aspx.cs" Inherits="WebFormsDemo.ListExample" %>
<!DOCTYPE html>
<html>
<head>
<title>List Example in ASP.NET</title>
</head>
<body>
<h2>List Example: Employee Details</h2>
<asp:Button ID="btnAdd" runat="server" Text="Add New Employee" OnClick="btnAdd_Click" />
<br /><br />
<asp:GridView ID="GridViewEmployees" runat="server" AutoGenerateColumns="true"></asp:GridView>
</body>
</html>
Code Behind (ListExample.aspx.cs)
using System;
using System.Collections.Generic;
using System.Data;
namespace WebFormsDemo
{
public partial class ListExample : System.Web.UI.Page
{
// Static list to hold employees (simulates session-based data)
static List<string> employeeList = new List<string> { "Sandhiya", "Karthik", "Priya", "Mani" };
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
// Method to Bind List to GridView
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("Employee Name");
foreach (string emp in employeeList)
{
dt.Rows.Add(emp);
}
GridViewEmployees.DataSource = dt;
GridViewEmployees.DataBind();
}
// Add new employee dynamically
protected void btnAdd_Click(object sender, EventArgs e)
{
employeeList.Add("New Employee " + (employeeList.Count + 1));
BindGrid();
}
}
}
Output
| Employee Name |
|---|
| Sandhiya |
| Karthik |
| Priya |
| Mani |
When you click “Add New Employee”, a new employee is dynamically added to the list.
Real-Time Use Case: Arrays vs Lists in ASP.NET
| Scenario | Best Choice | Reason |
|---|
| Fixed number of dropdown options (e.g., Gender, Country) | Array | Simple, small data, fixed values |
| Dynamic data (e.g., Products, Employees) | List | Data can grow or shrink at runtime |
| Data binding from DB results | List / DataTable | Easy integration with LINQ or GridView |
| Temporary in-memory storage | List | Supports add/remove easily |
Tip
If you’re loading data from a database:
List<string> productList = new List<string>();
using (SqlConnection con = new SqlConnection("your_connection_string"))
{
SqlCommand cmd = new SqlCommand("SELECT ProductName FROM Products", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
productList.Add(reader["ProductName"].ToString());
}
}
Then bind productList to a GridView or DropDownList.
Conclusion
Arrays and Lists are essential for handling collections of data in ASP.NET WebForms.
Both integrate smoothly with ASP.NET controls like GridView, DropDownList, and Repeater.