asp.net web窗体实现不需要登陆简单的学生管理系统
时间: 2023-06-11 19:07:59 浏览: 135
下面是一个简单的ASP.NET Web Forms学生管理系统的示例,不需要登录:
1. 创建一个新的ASP.NET Web Forms项目。
2. 在默认的Web窗体"Default.aspx"中添加一个GridView控件,它将显示学生的信息。在GridView的属性中,设置AutoGenerateColumns为"true"。
3. 在页面上添加一个TextBox和一个Button控件,用于添加新的学生信息。在Button的Click事件中,将TextBox中的数据添加到GridView中。
4. 在页面上添加一个DeleteButton控件,用于删除选定的学生信息。在DeleteButton的Click事件中,从GridView中删除选定的行。
5. 编写代码以连接到数据库,并在页面加载时从数据库中加载学生信息,并将其添加到GridView中。
下面是示例代码:
Default.aspx:
```asp
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add Student" OnClick="Button1_Click" />
<asp:Button ID="DeleteButton" runat="server" Text="Delete Selected Student" OnClick="DeleteButton_Click" />
```
Default.aspx.cs:
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadStudents();
}
}
private void LoadStudents()
{
string connectionString = "YourConnectionStringHere";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("SELECT * FROM Students", connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string studentName = TextBox1.Text;
string connectionString = "YourConnectionStringHere";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("INSERT INTO Students (Name) VALUES (@Name)", connection))
{
command.Parameters.AddWithValue("@Name", studentName);
command.ExecuteNonQuery();
}
}
LoadStudents();
}
protected void DeleteButton_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkBox = (CheckBox)row.FindControl("CheckBox1");
if (checkBox.Checked)
{
int studentId = Convert.ToInt32(row.Cells[1].Text);
string connectionString = "YourConnectionStringHere";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("DELETE FROM Students WHERE Id = @Id", connection))
{
command.Parameters.AddWithValue("@Id", studentId);
command.ExecuteNonQuery();
}
}
}
}
LoadStudents();
}
```
这只是一个简单的示例,您可以根据需要进行修改和扩展。
阅读全文