// 计算两个日期的时间间隔的小时数,DateTime1,DateTime2不论先后。随便放得出的结果都是正数。
public string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
double dateDiff = 0;
TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
TimeSpan ts = ts1.Subtract(ts2).Duration();
//dateDiff = ts.Days*24 + ts.Hours + ts.Minutes/60 + ts.Seconds/3600;
dateDiff = ts.TotalHours;
return dateDiff.ToString("f4");
}
============================================================================================
//得到当前的IIS用户
public string GetWebUserName()
{
string Login_User;
Login_User = System.Web.HttpContext.Current.Request.ServerVariables["LOGON_USER"].ToLower();
short Index = 0;
Index = (short)Login_User.IndexOf("\\");
if (Index > 0)
{
Login_User = Login_User.Substring(Index + 1);
}
else
{
Login_User = String.Empty;
}
return Login_User;
}
============================================================================================
//替代Gridview分页功能
public string WritePage(GridView TempGrid,int MaxPageCount)
{
//显示分页的信息(因为自带的只能显示部分功能)
//完全采用系统自带功能实现.
//在GridView的标签后面添加控件<table align=center width=98% height=26 valign=middle bgcolor=#e0e0e0 ><tr><td id=PageBar runat=server align=center></td></tr></table>
//再想办法得到未分页前的最大页数,然在Data_bound中添加这个函数 PageBar.InnerHtml = Base.Pub.Write(Gridview,MaxPage)
string Ret = string.Empty;
int RowCount = TempGrid.Rows.Count;
int PageIndex = TempGrid.PageIndex +1;
int PageCount = TempGrid.PageCount;
Ret = "<span style='font-size:9pt;'>总记录数:" + MaxPageCount.ToString() + " 每页:" + TempGrid.PageSize.ToString() + "条 页数:" + PageIndex.ToString() + "/" + PageCount.ToString() + " ";
if(PageIndex>1)
{
Ret += "<a href=" + "javascript:__doPostBack('" + TempGrid.ID + "','Page$First')" + ">首页</a> <a href=" + "javascript:__doPostBack('" + TempGrid.ID + "','Page$Prev')" + ">上页</a> ";
}
else
{
Ret += "首页 上页 ";
}
//------------------------------------------------------------------------------------
//此部分为中间的显示页码数,规则是当前页的前后五页
int StartPage,EndPage;
if(PageIndex>5)
{
StartPage = PageIndex -5;
}
else
{
StartPage = 1;
}
if(PageIndex +5 >PageCount)
{
EndPage = PageCount;
}
else
{
EndPage = PageIndex + 5;
}
for(int i=StartPage;i<EndPage;i++)
{
if(PageIndex ==i)
{
Ret += "<b>" +i.ToString()+ "</b> ";
}
else
{
Ret += "<a href="+"javascript:__doPostBack('" + TempGrid.ID + "','Page$" + i + "')"+">" + i + "</a>" + " ";
}
}
if(PageIndex<PageCount)
{
Ret += " <a href=" + "javascript:__doPostBack('" + TempGrid.ID + "','Page$Next')" + ">下页</a> <a href=" + "javascript:__doPostBack('" + TempGrid.ID + "','Page$Last')" + ">尾页</a>";
}
else
{
Ret += " 下页 尾页";
}
//--------------------------------------------------------------------------------------
//以下是输入页码,进行换行的部分'
Ret += " <input id='PageNum__Gpc' onkeydown='if(!KeyIsNumber(event.keyCode)) { return false; }' type='text' size='2' style='border-style:Groove;font-size:8pt;height:18px;width:25px;' /><input id='GOTO' type='button' value='GO' style='font-size:9pt;height:18px;width:26px;' onclick='CheckPageNum()' />";
//JAVASCRIPT部分,用来限制输入框的输入值,以及对输入值进行换页处理
Ret += "<script language='javascript'>";
Ret += " var MaxPage=" + PageCount.ToString() + ";";
Ret += "function KeyIsNumber(KeyCode)";
Ret += "{";
Ret += "if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46)||((KeyCode>95)&&(KeyCode<105)))";
Ret += "{ return true; }";
Ret += "else";
Ret += "{ return false; }";
Ret += "}";
Ret += "function CheckPageNum()";
Ret += "{ var obj = document.getElementById('PageNum__Gpc');";
Ret += "if ((obj.value!='')&&(obj.value!=0)&&(obj.value<=MaxPage))";
Ret += "{";
Ret += "try{";
Ret += "__doPostBack('" + TempGrid.ID + "','Page$'+obj.value); }";
Ret += "catch(err) {}";
Ret += "}";
Ret += "}" + "</script>";
//-------------------------------------------------------------------
return Ret + "</span>";
}
============================================================================================