前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TeachingPlan.aspx.cs" Inherits="WebUI.TeachingManagement.TeachingPlan" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity.SysManager;
using Business.SysManager;
using Commons;
using MySql.Data.MySqlClient;
using Business.OA;
namespace WebUI.TeachingManagement
{
public partial class TeachingPlan : System.Web.UI.Page
{
public UserBusi ub = new UserBusi();
public UserEntity user = new UserEntity();
protected void Page_Load(object sender, EventArgs e)
{
#region 用户登录信息验证
this.Page.Title = System.Configuration.ConfigurationManager.AppSettings["PageTitle"];
UserBusi userBusi = new UserBusi();
if (!userBusi.IsExistSession(ref user))
{
DbTools.MsgAndParentRedirect("请重新登录!", "../Default.aspx");
return;
}
#endregion
if (!IsPostBack)
{
BindSchoolYear();//绑定学年
TeacherBusi teaBusi = new TeacherBusi();
DataTable dt = getBoardTable("Grade", "t_teachingplan", "Grade<=9");
dt.Columns.Add("GradeChn", typeof(System.String));//用户姓名
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
dr["GradeChn"] = teaBusi.ChangeGrade(dr["Grade"].ToString());
}
ddlGrade.DataSource = dt.DefaultView;
ddlGrade.DataTextField = "GradeChn";
ddlGrade.DataValueField = "Grade";
ddlGrade.DataBind();
string sql = "select * from t_teachingplan where Course like '%" + ddlGrade.SelectedValue + "'";
Common comm = new Common();
DataSet ds = comm.GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow dr = ds.Tables[0].Rows[0];
string course=dr["Course"].ToString();
//把课程编号转化成课程名字
sql = "select * from tm_course where CourseCode='" + course + "' ";
ds = comm.GetDataSet(sql);
ddlCourse.DataSource = ds.Tables[0].DefaultView;
ddlCourse.DataTextField = "CourseName";
ddlCourse.DataValueField = "CourseCode";
ddlCourse.DataBind();
}
BindSchoolYear1();//绑定学年1
dt = getBoardTable("Grade", "c_gradeclassinfo", "Grade<=9");
dt.Columns.Add("GradeChn", typeof(System.String));//用户姓名
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
dr["GradeChn"] = teaBusi.ChangeGrade(dr["Grade"].ToString());
}
ddlGrade1.DataSource = dt.DefaultView;
ddlGrade1.DataTextField = "GradeChn";
ddlGrade1.DataValueField = "Grade";
ddlGrade1.DataBind();
sql = "select * from tm_course where CourseCode like '%" + ddlGrade1.SelectedValue + "'";
ds = comm.GetDataSet(sql);
ddlCourse1.DataSource = ds.Tables[0].DefaultView;
ddlCourse1.DataTextField = "CourseName";
ddlCourse1.DataValueField = "CourseCode";
ddlCourse1.DataBind();
#region 设置排序的默认字段
ViewState["SortOrder"] = "Id";
ViewState["OrderDire"] = "asc";
#endregion
ViewState["Searcher"] = " 1=1 ";
GridViewBind(ViewState["Searcher"].ToString());
//初始化添加用户信息
}
}
#region 绑定学年信息
private void BindSchoolYear()
{
int NowYear = DateTime.Now.Year;
for (int IntYear = NowYear - 5; IntYear <= NowYear; IntYear++)
{
string StrText = IntYear.ToString() + "-" + (IntYear + 1).ToString();
ddlSchoolYear.Items.Add(new ListItem(StrText, IntYear.ToString()));
}
if (DateTime.Now.Month >= 8)
{
ddlSchoolYear.SelectedValue = NowYear.ToString();
ddlSchoolTerm.SelectedValue = "1";
}
else
{
ddlSchoolYear.SelectedValue = (NowYear - 1).ToString();
ddlSchoolTerm.SelectedValue = "2";
}
newInit();
}
private void BindSchoolYear1()
{
int NowYear = DateTime.Now.Year;
for (int IntYear = NowYear - 5; IntYear <= NowYear; IntYear++)
{
string StrText = IntYear.ToString() + "-" + (IntYear + 1).ToString();
ddlSchoolYear1.Items.Add(new ListItem(StrText, IntYear.ToString()));
}
if (DateTime.Now.Month >= 8)
{
ddlSchoolYear1.SelectedValue = NowYear.ToString();
ddlSchoolTerm1.SelectedValue = "1";
}
else
{
ddlSchoolYear1.SelectedValue = (NowYear - 1).ToString();
ddlSchoolTerm1.SelectedValue = "2";
}
}
#endregion
#region getBoardTable: 读取Board数据表,得到DataTable
///
/// 读取S_BoardInfo数据表
///
///
private DataTable getBoardTable(string field, string table, string condition)
{
DataTable dtBoard;
string sql = "select distinct {0} from {1} where {2} order by {0}";
sql = string.Format(sql, field, table, condition);
Common comm = new Common();
dtBoard = comm.GetDataSet(sql).Tables[0];
return dtBoard;
}
#endregion
protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
{
TeacherBusi teaBusi = new TeacherBusi();
string sql = "select * from t_teachingplan where Course like '%" + ddlGrade.SelectedValue + "'";
Common comm = new Common();
DataSet ds = comm.GetDataSet(sql);
ddlCourse.DataSource = ds.Tables[0].DefaultView;
ddlCourse.DataTextField = "Course";
ddlCourse.DataValueField = "Course";
ddlCourse.DataBind();
}
protected void ddlGrade1_SelectedIndexChanged(object sender, EventArgs e)
{
TeacherBusi teaBusi = new TeacherBusi();
string sql = "select * from tm_course where CourseCode like '%" + ddlGrade1.SelectedValue + "'";
Common comm = new Common();
DataSet ds = comm.GetDataSet(sql);
ddlCourse1.DataSource = ds.Tables[0].DefaultView;
ddlCourse1.DataTextField = "CourseName";
ddlCourse1.DataValueField = "CourseCode";
ddlCourse1.DataBind();
}
#region GridView数据绑定事件
///
/// GridView数据绑定事件
///
private void GridViewBind(string condition)
{
string sql;
sql = "select * from `t_teachingplan` where {0} order by Id ";
sql = string.Format(sql, condition);
Common comm = new Common();
#region 分页控件对应代码
DataTable dt = comm.GetDataSet(sql).Tables[0];
// AspNetPager1.RecordCount = dt.Rows.Count;
// DataSet ds = comm.GetDataSet(sql, AspNetPager1.StartRecordIndex - 1, AspNetPager1.PageSize);
#endregion
#region
//if (dt.Rows.Count>0)
//{
// DataRow dr=dt.Rows[0];
// lbtp.Text=dr["SchoolYear"].ToString()+dr["SchoolTerm"].ToString()+dr["Grade"].ToString()+dr["Course"].ToString();
//}
#endregion
#region 实现单击列标题排序要用到的代码
DataView myView = dt.DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
myView.Sort = sort;
#endregion
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
#endregion
#region 分页控件的分页事件
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
GridViewBind(ViewState["Searcher"].ToString());
}
#endregion
#region 在单击某个用于对列进行排序
///
/// 在单击某个用于对列进行排序的超链接时发生,但在 GridView 控件执行排序操作之前。此事件通常用于取消排序操作或执行自定义的排序例程。
///
///
///
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "DESC")
ViewState["OrderDire"] = "ASC";
else
ViewState["OrderDire"] = "DESC";
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
GridViewBind(ViewState["Searcher"].ToString());
}
#endregion
#region 双击行的选择、鼠标颜色变化、行编辑、换页、取消行编辑、选择行编辑
///
/// 从写render实现双击选择行
///
///
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString(), true);
row.Attributes["style"] = "cursor:pointer";
row.Attributes["title"] = "双击编辑";
}
}
base.Render(writer);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "Cursor:hand";
e.Row.Attributes.Add("onmouseover", "e=this.style.backgroundColor; this.style.backgroundColor='#c8dafa'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=e");
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind(ViewState["Searcher"].ToString());
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
EditGridView();
}
#endregion
#region 插入记录初始化
///
/// 插入记录初始化
///
private void newInit()
{
// txtPersonName.Text = string.Empty;
// txtTestName.Text = string.Empty;
lnkSave.Text = "添加确认"; //更改按钮标题处于添加状态
// GridViewBind(ViewState["Searcher"].ToString());
}
#endregion
#region 删除记录和添加新记录 按钮事件
protected void lnkDel_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in this.GridView1.Rows)
{
CheckBox CheckRow = (CheckBox)row.FindControl("CheckRow");
if (CheckRow.Checked)
{
string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
Common comm = new Common();
comm.ExecuteNonQuery("delete from `t_teachingplan` where Id=" + id);
}
}
}
protected void lnkNew_Click(object sender, EventArgs e)
{
newInit();
}
#endregion
#region 编辑处理函数
///
/// 编辑处理函数
///
private void EditGridView()
{
//string ID = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
string ID=GridView1.DataKeys[GridView1.EditIndex].Value.ToString();
Common comm = new Common();
string sql = "select * from `t_teachingplan` where `Id`=" + ID;
DataSet ds = comm.GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow dr = ds.Tables[0].Rows[0];
newInit();
//关键字段,确定一个教学计划
ddlCourse1.SelectedValue = dr["Course"].ToString();
ddlGrade1.SelectedValue = dr["Grade"].ToString();
ddlSchoolTerm1.SelectedValue = dr["SchoolTerm"].ToString();
ddlSchoolYear1.SelectedValue = dr["SchoolYear"].ToString();
//教学计划内容
txtChapter.Text = dr["Chapter"].ToString();
txtContent.Text = dr["Content"].ToString();
txtTM.Text = dr["TeachinMmethods"].ToString();
txtWeek.Text = dr["Week"].ToString();
//ddlSubmit.SelectedValue = dr["Submit"].ToString();
hdfId.Value = dr["Id"].ToString();
lnkSave.Text = "保存修改";//改变按钮的名称
}
}
#endregion
#region 用户信息添加和修改
protected void lnkSave_Click(object sender, EventArgs e)
{
if (lnkSave.Text == "添加确认")
{
string sql = "select * from t_teachingplan";
Common comm = new Common();
DataSet ds = comm.GetDataSet(sql);
DataTable dt = ds.Tables[0];
DataRow dr = dt.NewRow();
SetDataRowValue(ref dr);
dt.Rows.Add(dr);
comm.InitComm();
MySqlDataAdapter adp = comm.GetAdapter(sql);
MySqlCommandBuilder comB = new MySqlCommandBuilder(adp);
if (adp.Update(ds, "ds") > 0)
DbTools.MsgBox("添加记录成功!");
ds.AcceptChanges();
newInit();
}
if (lnkSave.Text == "保存修改")
{
string sql = "select * from t_teachingplan where Id=" + hdfId.Value;
Common comm = new Common();
DataSet ds = comm.GetDataSet(sql);
DataTable dt = ds.Tables[0];
DataRow dr = dt.Rows[0];
SetDataRowValue(ref dr);
comm.InitComm();
MySqlDataAdapter adp = comm.GetAdapter(sql);
MySqlCommandBuilder comB = new MySqlCommandBuilder(adp);
if (adp.Update(ds, "ds") > 0)
DbTools.MsgBox("信息修改成功!");
ds.AcceptChanges();
GridViewBind(ViewState["Searcher"].ToString());
EditGridView();
}
}
///
/// 给DataRow赋值
///
///
private void SetDataRowValue(ref DataRow dr)
{
dr["SchoolTerm"] = ddlSchoolTerm1.SelectedValue;
dr["SchoolYear"] = ddlSchoolYear1.SelectedValue;
dr["Course"]=ddlCourse1.Text;
dr["Grade"] = ddlGrade1.SelectedValue;
//教学计划内容
dr["TeachinMmethods"] = txtWeek.Text;
dr["Content"] = txtContent.Text;
dr["Week"] = txtWeek.Text;
dr["Chapter"] = txtChapter.Text;
}
#endregion
#region 查询
protected void lnkSearch_Click(object sender, EventArgs e)
{
string condition = " 1=1 ";
string ch = "";
string id = "";
string id1 = "";
string sql = "";
string year = "";
if (ddlGrade.SelectedValue != "")
{
condition = condition + "and Grade like '%" + ddlGrade.SelectedValue + "%' ";
//id = ddlGrade.SelectedValue;
//Common com = new Common();
//sql = "select distinct Grade from t_teachingplan where Grade='" + id + "' order by Grade";
//DataSet ds = com.GetDataSet(sql);
//if (ds.Tables[0].Rows.Count > 0)
//{
// DataRow dr = ds.Tables[0].Rows[0];
// year = dr["Year"].ToString();
//}
}
if (ddlCourse.SelectedValue != "")
{
condition = condition + "and Course like '%" + ddlCourse.SelectedValue + "%' ";
}
if (ddlSchoolYear.SelectedValue != "")
{
condition = condition + "and SchoolYear like '%" + ddlSchoolYear.SelectedValue + "%' ";
}
if (ddlSchoolTerm.SelectedValue != "")
{
condition = condition + "and SchoolTerm like '%" + ddlSchoolTerm.SelectedValue + "%' ";
}
ViewState["Searcher"] = condition;
GridViewBind(ViewState["Searcher"].ToString());
}
#endregion
#region 显示全部
protected void lnkShowAll_Click(object sender, EventArgs e)
{
ViewState["Searcher"] = " 1=1 ";
GridViewBind(ViewState["Searcher"].ToString());
}
#endregion
#region 获得指定长度的字符串
public string getMyStr(string str, int a)
{
if (str.Length >= a)
return str.Substring(0, a);
else
return str;
}
#endregion
#region
#endregion
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//GridView1.PageIndex = e.NewEditIndex+1;
GridView1.EditIndex = e.NewEditIndex;
EditGridView();
}
}
}