Step 1: Design Page – ListOperations.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListOperations.aspx.cs" Inherits="ListOperations" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List Operations (Add, Remove, Search) - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f4f6fc;
margin: 50px;
}
.container {
width: 550px;
margin: auto;
background: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px #ccc;
padding: 25px;
}
h2 {
color: #1A2A80;
text-align: center;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
width: 32%;
}
.btn:hover {
background-color: #5b68a1;
}
.button-group {
display: flex;
justify-content: space-between;
margin-top: 15px;
}
.result {
margin-top: 20px;
font-weight: bold;
color: #333;
text-align: center;
white-space: pre-line;
}
hr {
margin-top: 15px;
border: 0.5px solid #ccc;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>List Operations (Add / Remove / Search)</h2>
<asp:Label ID="lblInput" runat="server" Text="Enter a Number:"></asp:Label><br />
<asp:TextBox ID="txtNumber" runat="server" CssClass="form-control" placeholder="Example: 25"></asp:TextBox>
<div class="button-group">
<asp:Button ID="btnAdd" runat="server" Text="Add" CssClass="btn" OnClick="btnAdd_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove" CssClass="btn" OnClick="btnRemove_Click" />
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="btn" OnClick="btnSearch_Click" />
</div>
<hr />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – ListOperations.aspx.cs
using System;
using System.Collections.Generic;
public partial class ListOperations : System.Web.UI.Page
{
// Using a static list to persist data across postbacks
private static List<int> numbers = new List<int>();
protected void Page_Load(object sender, EventArgs e)
{
}
// Add Element
protected void btnAdd_Click(object sender, EventArgs e)
{
if (int.TryParse(txtNumber.Text.Trim(), out int num))
{
numbers.Add(num);
lblResult.Text = $"Number {num} added successfully!\n\nCurrent List: {string.Join(", ", numbers)}";
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text = "Please enter a valid number.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
// Remove Element
protected void btnRemove_Click(object sender, EventArgs e)
{
if (int.TryParse(txtNumber.Text.Trim(), out int num))
{
if (numbers.Remove(num))
{
lblResult.Text = $"Number {num} removed successfully!\n\nCurrent List: {string.Join(", ", numbers)}";
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text = $"Number {num} not found in the list.";
lblResult.ForeColor = System.Drawing.Color.OrangeRed;
}
}
else
{
lblResult.Text = "Please enter a valid number.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
// Search Element
protected void btnSearch_Click(object sender, EventArgs e)
{
if (int.TryParse(txtNumber.Text.Trim(), out int num))
{
if (numbers.Contains(num))
{
lblResult.Text = $"Number {num} found in the list.\n\nCurrent List: {string.Join(", ", numbers)}";
lblResult.ForeColor = System.Drawing.Color.Green;
}
else
{
lblResult.Text = $"Number {num} not found.\n\nCurrent List: {string.Join(", ", numbers)}";
lblResult.ForeColor = System.Drawing.Color.OrangeRed;
}
}
else
{
lblResult.Text = "Please enter a valid number.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
}
Real-Time Example Flow
Run your project and open ListOperations.aspx.
Enter a number, e.g. 10, and click Add.
Output
Number 10 added successfully!
Current List: 10
Enter another number, e.g. 20, and click Add.
Output
Number 20 added successfully!
Current List: 10, 20
Enter 10 and click Search.
Output
Number 10 found in the list.
Current List: 10, 20
Enter 10 and click Remove.
Output
Number 10 removed successfully!
Current List: 20
Explanation
| Operation | Description |
|---|
| Add | Adds a new number to the List using List.Add(). |
| Remove | Removes a specific number using List.Remove(). |
| Search | Checks if a number exists in the List using List.Contains(). |
| Static List | Maintains the list data across postbacks (web page refreshes). |
Example Input / Output Table
| Operation | Input | Output |
|---|
| Add | 15 | Number 15 added successfully! Current List: 15 |
| Add | 30 | Number 30 added successfully! Current List: 15, 30 |
| Search | 15 | Number 15 found in the list. |
| Search | 25 | Number 25 not found. |
| Remove | 15 | Number 15 removed successfully! Current List: 30 |
Algorithm
1. Initialize an empty List<int>.
2. If Add:
Convert input to integer and add to list.
3. If Remove:
Check if number exists, then remove.
4. If Search:
Check if number exists using Contains().
5. Display updated list to user.