Chapter 6: WebPages Forms
WebPages Forms in the W3Schools WP Tutorial menu). This is where your pages stop being just “display stuff” and start interacting with users — collecting names, choices, messages, and making decisions based on what they type or select. 😄
Think of forms as the “conversation starter” between your website and the visitor. In real life: you ask “What’s your name?” → they answer → you say “Hi Rahul!”. In Web Pages: same thing, but with code.
Today we’ll go very detailed, step-by-step, like we’re building it together in Visual Studio or WebMatrix (or even Notepad + browser). I’ll use the classic W3Schools examples + extra explanations so you really get it.
1. Core Concept: How Forms Work in ASP.NET Web Pages
- Forms are pure HTML → <form>, <input>, <select>, <textarea>, etc.
- You set method=”post” (almost always — safer than “get”)
- action=”” or action=”samepage.cshtml” → submits back to the same page (postback pattern)
- When user clicks Submit → browser sends data to server via POST
- Server sees it’s a POST → IsPost becomes true
- You read data with Request[“name-of-field”] (or Request.Form[“…”])
- All values come as strings — convert if needed (.AsInt(), .AsDateTime(), etc.)
Golden Rule (write this down!) Always wrap your processing code inside:
|
0 1 2 3 4 5 6 7 8 9 10 |
if (IsPost) { // process form data here } else { // show empty form here } |
This way:
- First visit → clean form
- After submit → show results / thank you / errors
2. Example 1 – Basic Text Input Form (Classic W3Schools Style)
File: SimpleForm.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
@{ // Declare variables outside if-block so they're available everywhere var company = ""; var contact = ""; if (IsPost) { // Form was submitted → read values company = Request["CompanyName"]; contact = Request["ContactName"]; // Optional: trim whitespace company = company.Trim(); contact = contact.Trim(); // Here you could save to DB, send email, etc. } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Simple Form Example</title> </head> <body> <h2>Enter Company Details</h2> @if (IsPost) { <div style="color: green; font-weight: bold;"> <p>Thank you! You entered:</p> <p>Company Name: @company</p> <p>Contact Name: @contact</p> </div> <!-- Optional: link back to reset form --> <p><a href="">Enter new details</a></p> } else { <form method="post" action=""> Company Name:<br /> <input type="text" name="CompanyName" value="@company" size="40" /><br /><br /> Contact Name:<br /> <input type="text" name="ContactName" value="@contact" size="40" /><br /><br /> <input type="submit" value="Submit Information" /> </form> } </body> </html> |
Step-by-step what happens:
- Page loads first time → IsPost = false → shows empty <form>
- User types “Tata Consultancy” and “Rahul Sharma” → clicks Submit
- Browser POSTs to same page → IsPost = true
- Code reads Request[“CompanyName”] → “Tata Consultancy”
- Displays green thank-you message with values
- Variables keep values → if you want, you can pre-fill form again (good for “edit” mode)
Pro tip: Always use @ before variables in HTML — Razor escapes them safely (prevents XSS attacks).
3. Example 2 – Dropdown (Select) + Show Image Based on Choice
This is a super common real-world pattern (from W3Schools “Display Images” example).
File: ChoosePhoto.cshtml
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
@{ var imagePath = ""; // empty at start if (IsPost) { var choice = Request["Choice"]; if (!String.IsNullOrEmpty(choice)) { imagePath = "images/" + choice; // e.g., "images/Charminar.jpg" } } } <!DOCTYPE html> <html> <body> <h1>Choose a Hyderabad Landmark</h1> <form method="post" action=""> I want to see:<br /> <select name="Choice"> <option value="">-- Select one --</option> <option value="Charminar.jpg">Charminar</option> <option value="Golconda.jpg">Golconda Fort</option> <option value="HussainSagar.jpg">Hussain Sagar Lake</option> </select><br /><br /> <input type="submit" value="Show Photo" /> </form> @if (!String.IsNullOrEmpty(imagePath)) { <h3>Your selection:</h3> <img src="@imagePath" alt="Selected Landmark" style="max-width:500px;" /> <p><a href="">Choose another</a></p> } </body> </html> |
Key learning points here:
- <select name=”Choice”> → value goes to Request[“Choice”]
- Build dynamic <img src> using @imagePath
- Check !String.IsNullOrEmpty() before showing image (avoids broken image on first load)
- Put images in /images/ folder (logical path from root)
4. Bonus: Other Common Form Elements (You Should Know)
Add these to your forms:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!-- Radio buttons --> Gender:<br /> <input type="radio" name="Gender" value="Male" /> Male<br /> <input type="radio" name="Gender" value="Female" /> Female<br /> <!-- Checkbox --> <input type="checkbox" name="Newsletter" value="yes" /> Subscribe to newsletter<br /> <!-- Textarea --> Message:<br /> <textarea name="Message" rows="5" cols="40"></textarea><br /> <!-- Password (hidden input) --> Password:<br /> <input type="password" name="Pwd" /><br /> |
Read them same way:
|
0 1 2 3 4 5 6 7 8 |
var gender = Request["Gender"]; var wantsNewsletter = Request["Newsletter"] == "yes"; // checkbox sends value only if checked var message = Request["Message"]; |
5. Important Teacher Warnings & Best Practices (2026)
- Always use method=”post” for anything private (passwords, personal info) — “get” shows data in URL!
- Never trust user input → in real apps, validate/sanitize (we’ll see Helpers later)
- Empty fields → Request[“field”] returns empty string, not null
- Pre-fill form after submit (edit mode) → use value=”@variable” in inputs
- In modern ASP.NET Core → same ideas, but use Tag Helpers + Model Binding + [BindProperty]
- No built-in client validation here (old Web Pages) — later lessons cover Helpers for that
Summary – Closing the Notebook
ASP.NET Web Pages HTML Forms = standard HTML <form> + Razor power:
- IsPost to detect submit
- Request[“name”] to read data
- Same-page postback (action=””)
- Process in if (IsPost) → show results
- Display dynamic content (messages, images, etc.) based on input
This is the foundation for login forms, contact pages, calculators, quizzes — everything interactive!
Ready for next? Tell me:
- Want to add validation (required fields, numbers only)?
- Full contact form with email send (using WebMail)?
- Radio + checkbox processing example?
- Or jump to WebPages Objects (Request, Response deep dive)?
Your turn, Webliance — you’re building real skills here in Hyderabad! Keep it up! 🚀🇮🇳
