⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addachievement.ascx.cs

📁 学生成绩管理系统
💻 CS
字号:
using System;
using System.Data;
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;
using TeachHelper.BusinessLogicLayer;
using TeachHelper.Comm;
using System.Net.Mail;

public partial class TeachHelper_Controls_AddAchievement : System.Web.UI.UserControl
{

    #region 变量 

    int id;
    string name;
    string teacher;
    string department;
    int departmentId;
    int grade;
    string major;
    int majorId;
    int classes;
    string subject;
    int subjectId;
    string examType;
    int examTypeId;
    DateTime time;

    int point;

    #endregion

    #region 事件处理

    protected void Page_Load(object sender, EventArgs e)
    {
        //对于没有数字的内容,下面这行完全满足要求,但加了数字就不行,必须调用OnItemDataBound
        this.GridViewExamForYou.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");

        //下面这行是自动换行
        //GridViewExamForYou.Attributes.Add("style","word-break:break-all;word-wrap:break-word");


        if (!IsPostBack)
        {
            BindExamCatalog();            
        }
    }

    protected void ButtonExamInfo_Click(object sender, EventArgs e)
    {
        if (this.ButtonExamInfo.Text == "隐藏选择目录")
        {
            this.GridViewExamForYou.Visible = false;
            this.ButtonExamInfo.Text = "显示选择目录";
        }
        else
        {
            BindExamCatalog();
            this.GridViewExamForYou.Visible = true;
            this.ButtonExamInfo.Text = "隐藏选择目录";
        }
    }

    protected void GridViewExamForYou_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
        //{
        //    e.Row.Cells[1].Text = "<nobr>" + e.Row.Cells[1].Text + "</nobr>";
        //}
    }

    protected void GridViewExamForYou_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int index = Convert.ToInt32(e.NewSelectedIndex);
        GridViewRow row = GridViewExamForYou.Rows[index];
        id = Convert.ToInt32(Server.HtmlDecode(row.Cells[2].Text.Trim()));
        name = Server.HtmlDecode(row.Cells[3].Text.Trim());
        teacher = Server.HtmlDecode(row.Cells[4].Text.Trim());
        department = Server.HtmlDecode(row.Cells[5].Text.Trim());
        departmentId = Department.GetId(department);
        grade = Convert.ToInt32(Server.HtmlDecode(row.Cells[6].Text.Trim()));
        major = Server.HtmlDecode(row.Cells[7].Text.Trim());
        majorId = Major.GetId(major);
        classes = Convert.ToInt32(Server.HtmlDecode(row.Cells[8].Text.Trim()));
        subject = Server.HtmlDecode(row.Cells[9].Text);
        subjectId = Subject.GetId(subject);
        examType = Server.HtmlDecode(row.Cells[10].Text);
        examTypeId = ExamType.GetId(examType);
        time = Convert.ToDateTime(Server.HtmlDecode(row.Cells[11].Text));

        string username = Convert.ToString(DateTime.Now.Year - grade) + departmentId.ToString("000") + majorId.ToString("000") + classes.ToString("000");
        string info = string.Format("名称:{0};教师:{1};院系:{2};年级:{3};专业:{4};班级:{5};科目:{6};类型:{7};时间:{8}", name, teacher, department, grade.ToString(), major, classes.ToString(), subject, examType,time.ToLongDateString());
        this.LabelId.Text = id.ToString();
        this.LabelInfo.Text = info;        
        this.LabelPeasonNum.Text = username + "***";

        username += "[0-9][0-9][0-9]";
        this.DropDownListStudentsId.DataSource = Membership.FindUsersByName(username);
        this.DropDownListStudentsId.DataTextField = "UserName";
        this.DropDownListStudentsId.DataValueField = "UserName";
        this.DropDownListStudentsId.DataBind();

        ButtonExamInfo_Click(null, null);
        this.PanelInfo.Visible = true;
        this.MultiView1.ActiveViewIndex = 0;
    }

    protected void DropDownListStudentsId_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!this.CheckBoxStudentName.Checked)
        {
            return;
        }

        string username = this.DropDownListStudentsId.SelectedValue;
        string name = UserInfo.GetName(username);
        if (name == string.Empty)
        {
            name = "[没有资料]";
        }
        this.TextBoxName.Text = name;

        if (this.DropDownListStudentsId.SelectedIndex < this.DropDownListStudentsId.Items.Count - 1)
        {
            this.ButtonOkAndNext.Enabled = true;
        }
        else
        {
            this.ButtonOkAndNext.Enabled = false;
        }
    }

    protected void ButtonOkAndNext_Click(object sender, EventArgs e)
    {
        AddAchievement();
        this.DropDownListStudentsId.SelectedIndex = this.DropDownListStudentsId.SelectedIndex + 1;
    }

    protected void ButtonOK_Click(object sender, EventArgs e)
    { 
        AddAchievement();
    }

    protected void GridViewExamForYou_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int index = Convert.ToInt32(e.NewEditIndex);
        GridViewRow row = GridViewExamForYou.Rows[index];
        id = Convert.ToInt32(Server.HtmlDecode(row.Cells[2].Text.Trim()));
        name = Server.HtmlDecode(row.Cells[3].Text.Trim());
        teacher = Server.HtmlDecode(row.Cells[4].Text.Trim());
        department = Server.HtmlDecode(row.Cells[5].Text.Trim());
        departmentId = Department.GetId(department);
        grade = Convert.ToInt32(Server.HtmlDecode(row.Cells[6].Text.Trim()));
        major = Server.HtmlDecode(row.Cells[7].Text.Trim());
        majorId = Major.GetId(major);
        classes = Convert.ToInt32(Server.HtmlDecode(row.Cells[8].Text.Trim()));
        subject = Server.HtmlDecode(row.Cells[9].Text);
        subjectId = Subject.GetId(subject);
        examType = Server.HtmlDecode(row.Cells[10].Text);
        examTypeId = ExamType.GetId(examType);
        time = Convert.ToDateTime(Server.HtmlDecode(row.Cells[11].Text));

        string username = Convert.ToString(DateTime.Now.Year - grade) + departmentId.ToString("000") + majorId.ToString("000") + classes.ToString("000");
        string info = string.Format("名称:{0};教师:{1};院系:{2};年级:{3};专业:{4};班级:{5};科目:{6};类型:{7};时间:{8}", name, teacher, department, grade.ToString(), major, classes.ToString(), subject, examType, time.ToLongDateString());
        this.LabelId.Text = id.ToString();
        this.LabelInfo.Text = info;
        this.LabelPeasonNum.Text = username + "***";

        BindExam();

        ButtonExamInfo_Click(null, null);
        this.PanelInfo.Visible = true;
        this.MultiView1.ActiveViewIndex = 1;
    }

    protected void GridViewExamForYou_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridViewExamForYou.PageIndex = e.NewPageIndex;
        BindExamCatalog();
    }

    protected void GridViewExam_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridViewExam.EditIndex = e.NewEditIndex;
        BindExam();
    }

    protected void GridViewExam_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridViewExam.EditIndex = -1;
        BindExam();
    }

    protected void GridViewExam_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridViewExam.PageIndex = e.NewPageIndex;
        BindExam();
    }

    protected void GridViewExam_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = Convert.ToInt32(e.RowIndex);
        GridViewRow row = this.GridViewExam.Rows[index];
        int id = Convert.ToInt32(Server.HtmlDecode(row.Cells[0].Text.Trim()));
        string strPoint = ((TextBox)row.Cells[2].Controls[0]).Text.Trim();
        int point;
        try
        {
            point = Convert.ToInt32(strPoint);
        }
        catch (Exception ex)
        {
            MessageBox.Show(this.Page, "请输入一个有效的数字");
            return;
        }

        Achievement.UpdatePoint(id, point);

        this.GridViewExam.EditIndex = -1;
        BindExam();
    }

    protected void GridViewExam_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
       
    }

    protected void ButtonExportToExcel_Click(object sender, EventArgs e)
    {
        this.GridViewExam.Columns[0].Visible = false;
        this.GridViewExam.Columns[3].Visible = false;

        DataExport.ToExcel(GridViewExam, "成绩");

        this.GridViewExam.Columns[0].Visible = true;
        this.GridViewExam.Columns[3].Visible = true;
    }

    protected void DropDownListHigherPoint_SelectedIndexChanged(object sender, EventArgs e)
    {
        int examId = Convert.ToInt32(this.LabelId.Text.Trim());
        int point = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.LabelHigherNumber.Text = Achievement.GetNumberHigherPoint(examId, point).ToString();
    }

    protected void DropDownListLowerPoint_SelectedIndexChanged(object sender, EventArgs e)
    {
        int examId = Convert.ToInt32(this.LabelId.Text.Trim());
        int point = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.LabelLowerNumber.Text = Achievement.GetNumberLowerPoint(examId, point).ToString();
    }

    #endregion

    #region 私有 & 保护方法

    private void BindExamCatalog()
    {
        string username = Membership.GetUser().UserName;
        this.GridViewExamForYou.DataSource = Exam.GetCollectByTeacher(username);
        this.GridViewExamForYou.DataBind();
    }

    private void BindExam()
    {
        id = Convert.ToInt32(this.LabelId.Text.Trim());
        DataSet ds = Achievement.Get(id);
        if (ds.Tables[0].Rows.Count == 0)
        {
            this.LabelEditMessage.Text = "当前没有您要编辑的考试的内容,查添加数据请单击选择按钮";
            this.LabelEditMessage.Visible = true;
            this.PanelEdit.Visible = false;
        }
        else
        {
            this.LabelEditMessage.Visible = false;
            this.PanelEdit.Visible = true;
            this.GridViewExam.DataSource = ds;
            this.GridViewExam.DataBind();

            this.DropDownListLowerPoint.DataBind();
            this.DropDownListHigherPoint.DataBind();
            this.DropDownListLowerPoint.SelectedIndex = 60;
            this.DropDownListHigherPoint.SelectedIndex = 60;

            ViewAchievementInfo();
        }
    }

    private void ViewAchievementInfo()
    {
        id = Convert.ToInt32(this.LabelId.Text.Trim());
        this.LabelPersonNum.Text = Achievement.GetPensonCount(id).ToString();
        this.LabelHighestPoint.Text = Achievement.GetMaxPoint(id).ToString();
        this.LabelLowest.Text = Achievement.GetMinPoint(id).ToString();
        this.LabelAverage.Text = Achievement.GetAvgPoint(id).ToString();
    }

    private void AddAchievement()
    {
        id = Convert.ToInt32(this.LabelId.Text);
        point = Convert.ToInt32(this.TextBoxPoint.Text);
        name = this.DropDownListStudentsId.SelectedValue;
        try
        {
            Achievement.Add(name, point, id);

            if (this.CheckBoxEmail.Checked)
            {
                SendMailToStudent();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(this.Page, ex.Message);
        }
        this.TextBoxPoint.Text = string.Empty;
    }

    private void SendMailToStudent()
    {
        string studentId = this.DropDownListStudentsId.SelectedValue.Trim();
        string mailTo = Membership.GetUser(studentId).Email;
        string body = this.LabelInfo.Text + Environment.NewLine + Environment.NewLine + "分数:" + this.TextBoxPoint.Text;

        MailAddress to = new MailAddress(mailTo);
        MailAddress from = new MailAddress("teachhelper@163.com");
        MailMessage message = new MailMessage(from, to);
        message.Subject = "您的考试成绩";
        message.Body = body;
        // Use the application or machine configuration to get the 
        // host, port, and credentials.
        SmtpClient client = new SmtpClient();
        Console.WriteLine("Sending an e-mail message to {0} at {1} by using the SMTP host {2}.",
            to.User, to.Host, client.Host);
        client.Send(message);

    }

    #endregion

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -