ASP.
NET Programs in C# with Full Explanation
Program 1: ASP.NET Web Controls
a) Display "Hello World!" and a button that changes color on mouse hover:
File: Default.aspx
--------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hello World Page</title>
<style>
.hoverButton {
background-color: green;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
.hoverButton:hover {
background-color: yellow;
color: black;
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center; margin-top:50px;">
<asp:Label ID="lblMessage" runat="server" Text="Hello World!"></asp:Label>
<br /><br />
<button class="hoverButton">Hover me!</button>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
--------------------------------------------------
using System;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
// Optional server-side logic if needed
}
b) Display web controls: Centered button, label, and checkbox
File: WebControls.aspx
--------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebControls.aspx.cs"
Inherits="WebControls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Web Controls</title>
<style>
.centerDiv {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 90vh;
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="centerDiv">
<asp:Label ID="Label1" runat="server" Text="Hello"></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Click Me" OnClick="Button1_Click" /><br
/><br />
<asp:CheckBox ID="CheckBox1" runat="server" Text="Check me" />
</div>
</form>
</body>
</html>
File: WebControls.aspx.cs
--------------------------------------------------
using System;
public partial class WebControls : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
// Optional: Initialize values or visibility if needed
protected void Button1_Click(object sender, EventArgs e)
Label1.Text = "You clicked the button!";
}
Program 2: User Login Form using ASP.NET, ADO.NET & MySQL
Create MySQL table:
CREATE DATABASE userdb;
USE userdb;
CREATE TABLE Users (
ID INT AUTO_INCREMENT PRIMARY KEY,
User_Name VARCHAR(50),
Password VARCHAR(50)
);
Sample WebForm (Login.aspx):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center; padding-top:100px;">
<asp:Label ID="lblUser" runat="server" Text="User Name: "></asp:Label>
<asp:TextBox ID="txtUser" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="lblPass" runat="server" Text="Password: "></asp:Label>
<asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox><br /><br
/>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" /><br
/><br />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
</html>
Code-behind (Login.aspx.cs):
using System;
using System.Data;
using MySql.Data.MySqlClient;
public partial class Login : System.Web.UI.Page
protected void btnLogin_Click(object sender, EventArgs e)
string conStr = "server=localhost;user id=root;password=yourpassword;database=userdb";
using (MySqlConnection con = new MySqlConnection(conStr))
con.Open();
string query = "SELECT * FROM Users WHERE User_Name=@uname AND
Password=@pwd";
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.Parameters.AddWithValue("@uname", txtUser.Text);
cmd.Parameters.AddWithValue("@pwd", txtPass.Text);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
lblMessage.Text = "Login Successful!";
else
lblMessage.Text = "Invalid username or password.";