首页 新闻 搜索 专区 学院

请高手指点一下,我这样写代码是否规范?规范些的写法又是怎样的?

0
悬赏园豆:100 [已关闭问题] 关闭于 2010-06-24 08:59
代码
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());
}
}

 

小弟新学编程的,望各位大大多多指点.
非常感谢.

俺不是肥熊猫的主页 俺不是肥熊猫 | 初学一级 | 园豆:18
提问于:2010-06-12 11:07
< >
分享
所有回答(3)
0

只有一个方法中用到的变量就在方法中定义,不要移到类这一层面

对于类的成员最好显式声明可访问性

Gray Zhang | 园豆:17610 (专家六级) | 2010-06-12 11:10
0

1.为什么成员变量都定义在外面(多数你只使用了一次)?定义是最好初始化,剩得用时忘了而出错。

2.如果按照规范化来说的话,IF语句最好加上{}括号。

3.catch (Exception ex),不够精确。

Astar | 园豆:40805 (高人七级) | 2010-06-12 11:23
厄,是我没有说清楚. 应该有多个窗体要调用这类字段和方法 比如[系部][专业]等 所以,我把这些都封装到各个类里面去了.
支持(0) 反对(0) 俺不是肥熊猫 | 园豆:18 (初学一级) | 2010-06-12 11:27
0

SQL不要出现在页面代码里面

天堂口 | 园豆:514 (小虾三级) | 2010-06-12 11:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册