In ASP.NET, web page forms are used to collect and submit user input. Let's create a simple example of a web page with a form using ASP.NET Web Forms.
Example: Creating a Web Page Form in ASP.NET Web Forms
Set Up Your Project:
Design Your Web Page with a Form:
Default.aspx
in the Visual Studio designer.Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourProjectName._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Web Page with Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="txtName">Name:</label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<label for="txtEmail">Email:</label>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs
(code-behind file) in Visual Studio.Example
using System;
namespace YourProjectName
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Retrieve user input from textboxes
string name = txtName.Text;
string email = txtEmail.Text;
// Process user input (e.g., save to database, send email, etc.)
// For simplicity, let's just display a message
Response.Write($"Thank you, {name}, for submitting your email ({email})!");
}
}
}
Default.aspx
page in your browser.That's it! You've created a simple ASP.NET web page with a form. Users can enter their name and email, and server-side code processes the form submission. This example demonstrates the basic concept of using forms in ASP.NET to collect user input.