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;
public partial class lateCount : System.Web.UI.Page
{
//声明一个类
//该类用于操作数据集控件的数据填充
DbFill dbFill;
//声明一个类
//该类封装查询的各种条件的控件
LateControls controlsCollection;
//声明一个类
//该类封装系、专业、班级的DropDownList控件
DeptSpeClassForm deptSpeClassForm;
ImportToExcel importToExcel;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Page.Init += new EventHandler(Page_Init);
}
/// <summary>
/// 窗体初次载入时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Init(object sender, EventArgs e)
{
//实例化类
dbFill = new DbFill();
deptSpeClassForm = new DeptSpeClassForm(this.departDropDownList, this.specialDropDownList, this.classDropDownList);
#region 实例化WebControls类,并初始化
WebControls controls = new WebControls();
controls.RegionDropDownList = regionDropDownList;
controls.StartTimeTextBox = startTimeTextBox;
controls.EndTimeTextBox = endTimeTextBox;
controls.RoomTextBox = roomTextBox;
controls.NameTextBox = nameTextBox;
#endregion
controlsCollection = new LateControls(controls);
importToExcel = new ImportToExcel();
//调用方法,初始化数据
deptSpeClassForm.Page_Load();
controlsCollection.Page_Load();
}
/// <summary>
/// 按下查询Button,开始查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
string baseCondition;
protected void queryButton_Click(object sender, EventArgs e)
{
//查询语句
baseCondition = "select user_name,count(late.user_id) as late_count,class_name "
+ " from Region,Users,Class,Late "
+ " where region.region_id=late.region_id "
+ " and users.user_id=late.user_id "
+ " and users.class_id=class.class_id "
+ " and users.spc_id=class.spc_id ";
baseCondition += deptSpeClassForm.getQueryCondition();
baseCondition += controlsCollection.getQueryCondition();
baseCondition += "group by late.user_id,user_name,class_name";
baseCondition += " order by count(late.user_id) desc";
//判断时间转换的状态
if (controlsCollection.TimeState == 0) //转换失败
{
ClientScript.RegisterStartupScript(this.GetType(), "",
"<script>alert('请输入正确的时间')</script>");
}
else
{
//调用方法,将数据填充到lateCountGridView控件中
dbFill.fillDataGridView(lateCountGridView, baseCondition);
countLabel.Text = String.Format("总共有<span style='color:#FF0000 ;'>{0}</span>条数据", dbFill.Rows);
if (dbFill.Rows > 0)
{
saveButton.Enabled = true;
Session["baseCondition"] = baseCondition;
}
else saveButton.Enabled = false;
}
}
/// <summary>
/// 按下重置Button,初始化各控件的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void resetButton_Click(object sender, EventArgs e)
{
deptSpeClassForm.Page_Load();
controlsCollection.Page_Load();
}
/// <summary>
/// 将数据写入到Excel中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void saveButton_Click(object sender, EventArgs e)
{
//调用ImportToExcel类中方法
//将数据写入到Excel中
try
{
importToExcel.import(Session["baseCondition"].ToString(), "myExcel.xls", Page);
saveButton.Enabled = false;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.ToString());
}
}
小弟新学编程的,望各位大大多多指点.
非常感谢.
只有一个方法中用到的变量就在方法中定义,不要移到类这一层面
对于类的成员最好显式声明可访问性
1.为什么成员变量都定义在外面(多数你只使用了一次)?定义是最好初始化,剩得用时忘了而出错。
2.如果按照规范化来说的话,IF语句最好加上{}括号。
3.catch (Exception ex),不够精确。
SQL不要出现在页面代码里面