doapplicateovertime.aspx.cs
来自「一个小型的人事管理系统」· CS 代码 · 共 308 行
CS
308 行
using System;
using System.Data;
using System.Data.Sql;
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 ApplicateAction : System.Web.UI.Page
{
protected DataSet dsHolidaysBegin;//CalendarBegin里的法定假期集
protected DataSet dsHolidaysEnd;//CalendarEnd里的法定假期集
protected DateTime BeginTime;//CalendarBegin里的选定时间
protected DateTime EndTime;//CalendarEnd里的选定时间
protected TimeSpan ts;//时间跨度,以小时计
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CalendarBegin.VisibleDate = DateTime.Today;
CalendarEnd.VisibleDate = DateTime.Today;
}
FillHolidayDatasetBegin();
FillHolidayDatasetEnd();
}
protected void FillHolidayDatasetBegin()
{ //计算当前CalendarBegin显示的该月份的法定节假日集
//在切换日历月份、Page_Load事件后执行
DateTime firstDate = new DateTime(CalendarBegin.VisibleDate.Year,
CalendarBegin.VisibleDate.Month, 1);
DateTime lastDate = GetFirstDayOfNextMonthBegin();
dsHolidaysBegin = GetCurrentMonthData(firstDate, lastDate);
}
protected void FillHolidayDatasetEnd()
{//计算当前CalendarEnd显示的该月份的法定节假日集
//在切换日历月份、Page_Load事件后执行
DateTime firstDate = new DateTime(CalendarEnd.VisibleDate.Year,
CalendarEnd.VisibleDate.Month, 1);
DateTime lastDate = GetFirstDayOfNextMonthEnd();
dsHolidaysEnd = GetCurrentMonthData(firstDate, lastDate);
}
protected DateTime GetFirstDayOfNextMonthBegin()
{ //得到CalendarBegin当前下一月份的日期
int monthNumber, yearNumber;
if (CalendarBegin.VisibleDate.Month == 12)
{
monthNumber = 1;
yearNumber = CalendarBegin.VisibleDate.Year + 1;
}
else
{
monthNumber = CalendarBegin.VisibleDate.Month + 1;
yearNumber = CalendarBegin.VisibleDate.Year;
}
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
return lastDate;
}
protected DateTime GetFirstDayOfNextMonthEnd()
{//得到CalendarEnd当前下一月份的日期
int monthNumber, yearNumber;
if (CalendarEnd.VisibleDate.Month == 12)
{
monthNumber = 1;
yearNumber = CalendarEnd.VisibleDate.Year + 1;
}
else
{
monthNumber = CalendarEnd.VisibleDate.Month + 1;
yearNumber = CalendarEnd.VisibleDate.Year;
}
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
return lastDate;
}
protected DataSet GetCurrentMonthData(DateTime firstDate,
DateTime lastDate)
{ //查询数据库,返回在firstDate到lastDate之间的法定节假日集
DataSet dsMonth = new DataSet();
String connString = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTION"];
SqlConnection dbConnection = new SqlConnection(connString);
String query;
query = "SELECT HolidayDate FROM tblHoliday " +
" WHERE HolidayDate >= @firstDate AND HolidayDate < @lastDate";
SqlCommand dbCommand = new SqlCommand(query, dbConnection);
dbCommand.Parameters.Add(new SqlParameter("@firstDate",firstDate));
dbCommand.Parameters.Add(new SqlParameter("@lastDate", lastDate));
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand);
try
{
sqlDataAdapter.Fill(dsMonth);
}
catch {}
return dsMonth;
}
protected void CalendarBegin_DayRender(object sender, DayRenderEventArgs e)
{//填充CalendarBegin里表示法定节假日的格子的颜色
DateTime nextDate;
if (dsHolidaysBegin != null)
{
foreach (DataRow dr in dsHolidaysBegin.Tables[0].Rows)
{
nextDate = (DateTime)dr["HolidayDate"];
if (nextDate.Month == e.Day.Date.Month && nextDate.Day == e.Day.Date.Day)
{
e.Cell.BackColor = System.Drawing.Color.Blue;
}
}
}
}
protected void CalendarEnd_DayRender(object sender, DayRenderEventArgs e)
{//填充CalendarEnd里表示法定节假日的格子的颜色
DateTime nextDate;
if (dsHolidaysEnd != null)
{
foreach (DataRow dr in dsHolidaysEnd.Tables[0].Rows)
{
nextDate = (DateTime)dr["HolidayDate"];
if (nextDate.Month == e.Day.Date.Month && nextDate.Day == e.Day.Date.Day)
{
e.Cell.BackColor = System.Drawing.Color.Blue;
}
}
}
}
protected void CalendarBegin_VisibleMonthChanged(object sender,
MonthChangedEventArgs e)
{//当CalendarBegin的月份切换时,要重新计算新月份的法定节假日
FillHolidayDatasetBegin();
FillHolidayDatasetEnd();
}
protected void CalendarEnd_VisibleMonthChanged(object sender,
MonthChangedEventArgs e)
{//当CalendarEnd的月份切换时,要重新计算新月份的法定节假日
FillHolidayDatasetBegin();
FillHolidayDatasetEnd();
}
protected void CalendarBegin_SelectionChanged(object sender, EventArgs e)
{ //每次单击CalendarBegin时,TimeBegin下拉框的选项要重新添加
DateTime SelectedDateBegin = CalendarBegin.SelectedDate;
DateTime nextDate;
int flag=0,i;
if (dsHolidaysBegin != null)
{
foreach (DataRow dr in dsHolidaysBegin.Tables[0].Rows)
{
nextDate = (DateTime)dr["HolidayDate"];
if (nextDate.Month == SelectedDateBegin.Month && nextDate.Day == SelectedDateBegin.Day)
{
flag = 1;
}
}
if (0 == flag) flag = 2;
}
TimeBegin.Items.Clear();
if (1 == flag)
{
for (i = 9; i <= 17; i++)
TimeBegin.Items.Add(new ListItem(i.ToString() + ":00",i.ToString()));
TimeBegin.SelectedIndex = 0;
}
else if (2 == flag)
{
for (i = 0; i <= 8; i++)
TimeBegin.Items.Add(new ListItem(i.ToString() + ":00", i.ToString()));
for (i = 18; i <= 23; i++)
TimeBegin.Items.Add(new ListItem(i.ToString() + ":00", i.ToString()));
TimeBegin.SelectedIndex = 0;
}
}
protected void CalendarEnd_SelectionChanged(object sender, EventArgs e)
{//每次单击CalendarEnd时,TimeEnd下拉框的选项要重新添加
DateTime SelectedDateEnd = CalendarEnd.SelectedDate;
DateTime nextDate;
int flag = 0, i;
if (dsHolidaysEnd != null)
{
foreach (DataRow dr in dsHolidaysEnd.Tables[0].Rows)
{
nextDate = (DateTime)dr["HolidayDate"];
if (nextDate.Month == SelectedDateEnd.Month && nextDate.Day == SelectedDateEnd.Day)
{
flag = 1;//一种情形
}
}
if (0 == flag) flag = 2;//另一种情形
}
TimeEnd.Items.Clear();
if (1 == flag)
{
for (i = 10; i <= 18; i++)
TimeEnd.Items.Add(new ListItem(i.ToString() + ":00", i.ToString()));
TimeEnd.SelectedIndex = TimeEnd.Items.Count-1;
}
else if (2 == flag)
{
for (i = 0; i <= 9; i++)
TimeEnd.Items.Add(new ListItem(i.ToString() + ":00", i.ToString()));
for (i = 19; i <= 23; i++)
TimeEnd.Items.Add(new ListItem(i.ToString() + ":00", i.ToString()));
TimeEnd.SelectedIndex = 0;
}
}
protected void CalOvertime_Click(object sender, EventArgs e)
{ //单击"计算时间"按钮
BeginTime = CalendarBegin.SelectedDate;
BeginTime=BeginTime.AddHours(double.Parse(TimeBegin.SelectedValue.ToString()));//得到加班开始时间
EndTime = CalendarEnd.SelectedDate;
EndTime=EndTime.AddHours(double.Parse(TimeEnd.SelectedValue.ToString()));//得到加班结束时间
ts = EndTime - BeginTime;//求得加班总时间
if (OvertimtReason.Text == "")
{
LabelAlert.Visible = true;
LabelAlert.Text = "加班理由不能为空";
LabelOvertime.Visible = false;
}
else if (ts.TotalHours <= 0)
{
LabelAlert.Visible = true;
LabelAlert.Text = "请检查输入日期的正确性";
LabelOvertime.Visible = false;
}
else
{
LabelAlert.Visible = false;
LabelOvertime.Visible = true;
LabelOvertime.Text = ts.TotalHours.ToString();
}
}
protected void AddOvertimeApplicate()
{
string strconn = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTION"];
string strsql = "Insert into tblOvertime ("
+ "EmployeeID,ApproverID,"
+ "SubmitTime,StartTime,"
+ "EndTime,Reason,"
+ "Status,Type,Hours) values"
+ "('" + ChooseApprover.SelectedValue.ToString() + "','" + Session["EmployeeID"].ToString() + "','" +
DateTime.Today.ToString() + "','" + BeginTime + "','" +
EndTime + "','" + OvertimtReason.Text + "'," +
"'已提交','" + ChooseType.SelectedValue.ToString() + "','"+ts.TotalHours.ToString()+"')";
//注意,这个SQL语句用到了Session对象里的信息
SqlConnection myconn = new SqlConnection(strconn);
SqlCommand mycmd = new SqlCommand(strsql,myconn);
myconn.Open();
mycmd.ExecuteNonQuery();
myconn.Close();
}
protected void OvertimeApplicateOK_Click(object sender, EventArgs e)
{
BeginTime = CalendarBegin.SelectedDate;
BeginTime = BeginTime.AddHours(double.Parse(TimeBegin.SelectedValue.ToString()));//加班开始时间
EndTime = CalendarEnd.SelectedDate;
EndTime = EndTime.AddHours(double.Parse(TimeEnd.SelectedValue.ToString()));//加班结束时间
ts = EndTime - BeginTime;
if (OvertimtReason.Text == "")
{
LabelAlert.Visible = true;
LabelAlert.Text = "加班理由不能为空";
LabelOvertime.Visible = false;
}
else if (ts.TotalHours <= 0)
{
LabelAlert.Visible = true;
LabelAlert.Text = "请检查输入日期的正确性";
LabelOvertime.Visible = false;
}
else if (ChooseApprover.SelectedValue.ToString() == "")
{
LabelAlert.Visible = true;
LabelAlert.Text = "请选择审批者";
LabelOvertime.Visible = false;
}
else if (ChooseType.SelectedValue.ToString() == "")
{
LabelAlert.Visible = true;
LabelAlert.Text = "请选择加班方式";
LabelOvertime.Visible = false;
}
else
{
AddOvertimeApplicate();
Response.Write("<script>alert('加班申请成功!')</script>");
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?