asp.net web窗体实习简单不需要登陆的学生信息管理系统
时间: 2023-06-11 19:08:15 浏览: 143
以下是一个简单的ASP.NET Web Forms学生信息管理系统示例,无需登录:
1. 首先,创建一个ASP.NET Web Forms项目。
2. 在Web应用程序中创建一个名为“Student”(或类似名称)的文件夹。在此文件夹中,创建一个名为“AddStudent.aspx”的新Web表单。
3. 将以下代码添加到AddStudent.aspx文件中:
```html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddStudent.aspx.cs" Inherits="WebApplication1.Student.AddStudent" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add Student</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label>First Name:</label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<br />
<label>Last Name:</label>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<br />
<label>Age:</label>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<br />
<label>Address:</label>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<<br />
<asp:Button ID="btnAdd" runat="server" Text="Add Student" OnClick="btnAdd_Click" />
</div>
</form>
</body>
</html>
```
4. 在“Student”文件夹中创建一个名为“Student.cs”的新类文件,并将以下代码添加到该文件中:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Student
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
}
```
5. 在“Student”文件夹中创建一个名为“StudentList.aspx”的新Web表单,并将以下代码添加到该文件中:
```html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentList.aspx.cs" Inherits="WebApplication1.Student.StudentList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Student List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Student List</h2>
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
```
6. 在“Student”文件夹中创建一个名为“StudentList.aspx.cs”的新类文件,并将以下代码添加到该文件中:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1.Student
{
public partial class StudentList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
List<Student> students = (List<Student>)Session["Students"];
if (students != null && students.Count > 0)
{
gvStudents.DataSource = students;
gvStudents.DataBind();
}
}
}
}
```
7. 添加一个名为“AddStudent.aspx.cs”的新类文件,并将以下代码添加到该文件中:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1.Student
{
public partial class AddStudent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
List<Student> students = (List<Student>)Session["Students"];
if (students == null)
{
students = new List<Student>();
}
Student student = new Student()
{
FirstName = txtFirstName.Text.Trim(),
LastName = txtLastName.Text.Trim(),
Age = int.Parse(txtAge.Text.Trim()),
Address = txtAddress.Text.Trim()
};
students.Add(student);
Session["Students"] = students;
Response.Redirect("~/Student/StudentList.aspx");
}
}
}
```
8. 运行应用程序,并在浏览器中导航到“AddStudent.aspx”页面。在此页面上,输入学生的详细信息,然后单击“Add Student”按钮。学生信息将被添加到应用程序中,并在“StudentList.aspx”页面上显示。
这是一个非常基本的示例,但可以作为起点来开发更复杂的学生信息管理系统。
阅读全文