Getting Started
2 snippetsASP.NET Web Forms basics
Page Directive
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="MyApp.Default" %>Basic Page Structure
<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>My Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage" runat="server"
Text="Hello, World!" />
</div>
</form>
</body>
</html>Server Controls
3 snippetsASP.NET Web Server Controls
Basic Controls
<asp:Label ID="lblName" runat="server" Text="Name:" />
<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />DropDownList
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Value="US">United States</asp:ListItem>
<asp:ListItem Value="CA">Canada</asp:ListItem>
<asp:ListItem Value="UK">United Kingdom</asp:ListItem>
</asp:DropDownList>GridView
<asp:GridView ID="gvCustomers" runat="server"
AutoGenerateColumns="False" DataKeyNames="CustomerId">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:CommandField ShowEditButton="True"
ShowDeleteButton="True" />
</Columns>
</asp:GridView>Code-Behind (C#)
3 snippetsServer-side event handling
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblMessage.Text = "Welcome!";
BindData();
}
}Button Click
protected void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string email = txtEmail.Text;
lblResult.Text = $"Hello, {name}!";
}GridView Events
protected void gvCustomers_RowEditing(object sender,
GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
BindData();
}
protected void gvCustomers_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
int customerId = Convert.ToInt32(gvCustomers.DataKeys[e.RowIndex].Value);
DeleteCustomer(customerId);
BindData();
}Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Data Binding
2 snippetsBinding data to controls
Manual Binding
private void BindData()
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string sql = "SELECT * FROM Customers";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDB %>"
SelectCommand="SELECT * FROM Customers"
UpdateCommand="UPDATE Customers SET Name=@Name WHERE Id=@Id"
DeleteCommand="DELETE FROM Customers WHERE Id=@Id" />
<asp:GridView ID="gvCustomers" runat="server"
DataSourceID="SqlDataSource1" />Validation Controls
4 snippetsClient and server-side validation
Required Field
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
ControlToValidate="txtEmail"
ErrorMessage="Email is required"
Display="Dynamic" />RegularExpression
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail"
ValidationExpression="\w+@\w+\.\w+"
ErrorMessage="Invalid email format"
Display="Dynamic" />Range Validator
<asp:TextBox ID="txtAge" runat="server" />
<asp:RangeValidator ID="rvAge" runat="server"
ControlToValidate="txtAge"
MinimumValue="18"
MaximumValue="100"
Type="Integer"
ErrorMessage="Age must be 18-100" />Validation Summary
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Please correct the following errors:"
DisplayMode="BulletList"
ShowSummary="True" />State Management
3 snippetsViewState, Session, and Application
ViewState
// Store in ViewState
ViewState["Counter"] = 10;
// Retrieve from ViewState
if (ViewState["Counter"] != null)
{
int counter = (int)ViewState["Counter"];
}Session
// Store in Session
Session["Username"] = "JohnDoe";
Session["UserId"] = 1001;
// Retrieve from Session
if (Session["Username"] != null)
{
string username = Session["Username"].ToString();
}Application
// Store in Application (global)
Application.Lock();
Application["TotalVisits"] = (int)Application["TotalVisits"] + 1;
Application.UnLock();
// Retrieve from Application
int visits = (int)Application["TotalVisits"];Master Pages
2 snippetsReusable page templates
Master Page
<%@ Master Language="C#" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>My Site</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div id="header">
<h1>My Website</h1>
</div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<div id="footer">
© 2024 My Company
</div>
</body>
</html>Content Page
<%@ Page Language="C#" MasterPageFile="~/Site.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style>
.custom { color: blue; }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
runat="server">
<h2>Welcome</h2>
<p>This is the content page.</p>
</asp:Content>User Controls
2 snippetsReusable components
User Control (.ascx)
<%@ Control Language="C#" ClassName="ProductCard" %>
<div class="product-card">
<asp:Label ID="lblName" runat="server" />
<asp:Label ID="lblPrice" runat="server" />
<asp:Button ID="btnAddToCart" runat="server"
Text="Add to Cart" OnClick="btnAddToCart_Click" />
</div>Using User Control
<%@ Register Src="~/Controls/ProductCard.ascx"
TagPrefix="uc" TagName="ProductCard" %>
<uc:ProductCard ID="ProductCard1" runat="server" />