AI Generate ASP.NET docs instantly

ASP.NET Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

2 snippets

ASP.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 snippets

ASP.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 snippets

Server-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.

Try Free

Data Binding

2 snippets

Binding 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 snippets

Client 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 snippets

ViewState, 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 snippets

Reusable 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 snippets

Reusable 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" />

More Cheat Sheets

FAQ

Frequently asked questions

What is a ASP.NET cheat sheet?

A ASP.NET cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in ASP.NET. It helps developers quickly look up syntax without searching through documentation.

How do I learn ASP.NET quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important ASP.NET concepts?

Key ASP.NET concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my ASP.NET code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your ASP.NET code using AI.

Code Conversion Tools

Convert ASP.NET to Other Languages

Easily translate your ASP.NET code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate ASP.NET Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week