实例介绍
【实例简介】
管理员可分别对学生信息进行管理,成绩进行管理,在学生信息管理模块有添加学生信息,删除学生信息,修改学生信息,可根据学号或姓名模糊查询学生信息。
在成绩管理模块,可添加已有学生的考试信息及相关删除学生有关成绩信息,根据姓名模糊查询学生成绩修改学生的考试信息及成绩等。
【实例截图】
【核心代码】
学生管理模块
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentManger.aspx.cs" Inherits="Admin_StudentManger" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="../bootstrap-3.4.1-dist/css/bootstrap.css" rel="stylesheet" />
<script src="../bootstrap-3.4.1-dist/js/bootstrap.js"></script>
</head>
<body>
<form id="form1" runat="server">
<br /> <br />
<div>
<asp:Button ID="Button1" runat="server" BackColor="#0099FF" BorderColor="#6699FF" ForeColor="White" Text=" 新增" OnClick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>学号</asp:ListItem>
<asp:ListItem>姓名</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" BackColor="#0099FF" BorderColor="#6699FF" ForeColor="White" Text="查询" OnClick="Button2_Click" />
<br />
</div>
<p>
<asp:GridView ID="GridView1" runat="server" CssClass="table table-hover table-bordered" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</p>
</form>
</body>
</html>
public partial class Admin_StudentManger : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public void bind() //数据绑定
{
Pub.db = new DataClassesDataContext();//保证数据是最新的
GridView1.DataSource = Pub.db.student;
GridView1.DataKeyNames = new string[] { "sno" };
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sno = GridView1.DataKeys[e.RowIndex].Value.ToString();
student delstudent = Pub.db.student.Where(a => a.sno == sno).FirstOrDefault();
Pub.db.student.DeleteOnSubmit(delstudent);
Pub.db.SubmitChanges();//把所作的更改都提交了
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string sno = GridView1.DataKeys[e.RowIndex].Value.ToString();//找到主键在哪
student updatestudent = Pub.db.student.Where(a => a.sno == sno).FirstOrDefault();//定位到哪行
updatestudent.sname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
updatestudent.sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
updatestudent.pwd = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
updatestudent.birthday = Convert.ToDateTime(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString());
Pub.db.SubmitChanges();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;//所有行不属于编辑状态
bind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "")
{
GridView1.DataSource = Pub.db.student;//所有数据展示出来
GridView1.DataBind();
return;
}
switch (DropDownList1.Text)
{
case "学号":
GridView1.DataSource = Pub.db.student.Where(a => a.sno == TextBox1.Text.Trim());
GridView1.DataBind();
break;
case "姓名":
GridView1.DataSource = Pub.db.student.Where(a => a.sname.Contains(TextBox1.Text.Trim()));
GridView1.DataBind();
break;
default:
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("studentAdd.aspx");
}
}
课程管理模块
public partial class Admin_CourseManger : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)//防止自动刷新
{
bind();
setctrls(false);
}
}
//数据绑定
public void bind()
{
Pub.db = new DataClassesDataContext();//保证数据是最新的
GridView1.DataSource = Pub.db.course;
GridView1.DataKeyNames = new string[] { "cno" };//先说明主键
GridView1.DataBind();//重新绑定一下
DropDownList2.DataSource = Pub.db.teacher;
DropDownList2.DataTextField = "tname";//显示在文本框
DropDownList2.DataValueField = "tno";
DropDownList2.DataBind();//重新绑定一下
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string cno = GridView1.DataKeys[e.RowIndex].Value.ToString();//找到主键在哪
course delcourse = Pub.db.course.Where(a => a.cno == cno).FirstOrDefault();
Pub.db.course.DeleteOnSubmit(delcourse);
Pub.db.SubmitChanges();
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string cno = GridView1.DataKeys[e.RowIndex].Value.ToString();//找到主键在哪
course updatecourse = Pub.db.course.Where(a => a.cno == cno).FirstOrDefault();//定位到哪行
updatecourse.cname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
updatecourse.tno = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
updatecourse.xs = Convert.ToByte(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString());
updatecourse.skdd= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
Pub.db.SubmitChanges();
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;//所有行不属于编辑状态
bind();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() == "")
{
GridView1.DataSource = Pub.db.course;//所有数据展示出来
GridView1.DataBind();
return;
}
switch (DropDownList1.Text)
{
case "课程号":
GridView1.DataSource = Pub.db.course.Where(a => a.cno == TextBox1.Text.Trim());
GridView1.DataBind();
break;
case "课程名":
GridView1.DataSource = Pub.db.course.Where(a => a.cname.Contains( TextBox1.Text.Trim()));
GridView1.DataBind();
break;
case "学时":
GridView1.DataSource = Pub.db.course.Where(a => a.xs == Convert.ToByte(TextBox1.Text.Trim()));
GridView1.DataBind();
break;
default:
break;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("CourseAdd.aspx");
}
public void setctrls(bool set)
{
Label1.Visible = set;
Label2.Visible = set;
Label3.Visible = set;
Label4.Visible = set;
Label5.Visible = set;
TextBox4.Visible = set;
TextBox5.Visible = set;
TextBox7.Visible = set;
TextBox8.Visible = set;
DropDownList2.Visible = set;
Button4.Visible = set;
}
protected void Button4_Click(object sender, EventArgs e)
{
if (TextBox4.Text.Trim() == "" || TextBox5.Text.Trim() == "" || TextBox7.Text.Trim() == "" || TextBox8.Text.Trim() == "")
{
WebMessageBox.Show("字段为空!");
return;
}
if (Pub.db.course.Where(a => a.cno == TextBox4.Text.Trim()).Count() >= 1)
{
WebMessageBox.Show("课程号不能重复!");
return;
}
course newcourse = new course();
newcourse.cno = TextBox4.Text.Trim();
newcourse.cname = TextBox5.Text.Trim();
newcourse.xs = Convert.ToByte(TextBox7.Text);
newcourse.skdd = TextBox8.Text.Trim();
newcourse.tno = DropDownList2.SelectedValue.ToString();
Pub.db.course.InsertOnSubmit(newcourse);
Pub.db.SubmitChanges();
setctrls(false);
bind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CourseManger.aspx.cs" Inherits="Admin_CourseManger" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="../bootstrap-3.4.1-dist/css/bootstrap.css" rel="stylesheet" />
<script src="../bootstrap-3.4.1-dist/js/bootstrap.js"></script>
<style type="text/css">
.auto-style1 {
width: 500px;
}
.auto-style2 {
height: 27px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" 添加" BackColor="#0099FF" BorderColor="#6699FF" ForeColor="White" Width="54px" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>课程号</asp:ListItem>
<asp:ListItem>课程名</asp:ListItem>
<asp:ListItem>学时</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="查询" BackColor="#0099FF" BorderColor="#6699FF" ForeColor="White" />
<br />
<br />
<asp:GridView ID="GridView1" CssClass="table table-bordered table-hover" Width="1332px" runat="server" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</div>
<br />
<br />
<br />
<table class="auto-style1">
<tr>
<td style="text-align: right">
<asp:Label ID="Label1" runat="server" Text="课程号"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2" style="text-align: right">
<asp:Label ID="Label2" runat="server" Text="课程名"></asp:Label>
</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
<asp:Label ID="Label3" runat="server" Text="授课教师"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="text-align: right">
<asp:Label ID="Label4" runat="server" Text="学时"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right">
<asp:Label ID="Label5" runat="server" Text="授课地点"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论