📄 teachersubmit.aspx.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class teachersubmit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] == null)//如果没有登录,转向登录界面
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
{
ImageButton1.Attributes.Add("OnClick", "javascript:return confirm('您只能提交一次成绩,确实要提交吗?')"); //成绩只能提交一次,给出提示
BindDDL();
bind();
}//绑定GridView
}
}
private void bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);//创建连接对象
string ValidateStr = "select max(Score) as score from SC where Cno='" + ddlCourse.SelectedValue + "' and Teaid='" + Session["Username"].ToString() + "'";
if (conn.State.ToString() == "Closed") //连接如果关闭,打开
conn.Open();
SqlCommand cmd = new SqlCommand(ValidateStr, conn); //创建操作数据库对象
SqlDataReader sdr = cmd.ExecuteReader(); //执行查询
if (sdr.Read())
{
if (sdr["score"].ToString() != "") //判断成绩是否提交
{
GridView1.Visible = false;
Response.Write("<script language=javascript>alert('该课程成绩已经提交,不能再次提交!');window.location.href='teacher.aspx';</script>");
}
else
{
sdr.Close();
GridView1.Visible = true;
string SqlStr = "SELECT student.*,SC.* FROM Student ,SC where Student.Sno=SC.Sno and SC.Cno='" + ddlCourse.SelectedValue + "'order by Student.Sno, Student.Grade,Student.Class";
DataSet ds = new DataSet();
try
{
if (conn.State.ToString() == "Closed") //连接如果关闭,打开
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds); //向DataSet数据集填充数据
GridView1.DataSource = ds.Tables[0].DefaultView;//为GridView指名数据源
GridView1.DataBind(); //绑定数据
}
catch (Exception ex) //异常处理
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open") //如果连接打开,关闭
conn.Close();
//}
}
}
}
}
private void BindDDL()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
string SqlStr = "SELECT distinct Course.Cno,Course.Cname from SC,Course where SC.Cno=Course.Cno and SC.Teaid='" + Session["userName"].ToString() + "'";
DataSet ds = new DataSet();
if (conn.State.ToString() == "Closed") //如果连接关闭,打开
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds); //从数据库中取数据放到DataSet数据集中
if (conn.State.ToString() == "Open")
conn.Close();
ddlCourse.DataSource = ds.Tables[0].DefaultView;
ddlCourse.DataTextField = "Cname"; //下拉列表框每项显示课程名称
ddlCourse.DataValueField = "Cno"; //下拉列表框每项的值为课程编号
ddlCourse.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
string strUpdate = "";
int score;
string stuID = "";
string courceID = ddlCourse.SelectedValue; //课程编号
int flag = 0; //标志
string Teaid = Session["Username"].ToString();
if (conn.State.ToString() == "Closed") //连接如果关闭,打开
conn.Open();
int i;
for (i = 0; i < GridView1.Rows.Count; i++)
{
score = int.Parse(((TextBox)GridView1.Rows[i].FindControl("txtScore")).Text.Trim());//取出学生成绩
stuID = GridView1.Rows[i].Cells[0].Text;
strUpdate = "update SC set Score=" + score + " where Sno='" + stuID + "' and Cno='" + courceID + "' and Teaid='" + Teaid + "'";
try
{
SqlCommand cmd = new SqlCommand(strUpdate, conn);//创建Command对象
flag = cmd.ExecuteNonQuery(); //执行添加成绩操作
}
catch (Exception ex) //异常处理
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
if (conn.State.ToString() == "Open") //如果连接打开,关闭
conn.Close();
if ((flag == 1) && (i == GridView1.Rows.Count))
{
Response.Write("成功录入学生成绩!此后将不能再修改!");
Response.Redirect("teacher.aspx");
}
}
protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
{
bind();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -