一:使用DataGrid的模板列传递多个参数到另一个页面中。
在Asp.Net中使用超级链接列不能传递多个参数到另一个页面中,要实现该功能,则要将超级链接列转换成模板列。
1:如果多个参数都是数据表的不同列的值,那么在HTML中通过DataBinder.Eval(Container,"DataTable.DataField")方法直接写即可。
如:
NavigateUrl='<%#"QualityLevelQueryDetail.aspx?Employee_Code="+DataBinder.Eval(Container.DataItem,"Employee_Code")+"&Employee_Name="+DataBinder.Eval(Container.DataItem,"Employee_Name")%>'>
2:如果多个参数并不都是从数据表中取的值,比如,在某个查询页面,先按日期范围取出记录,然后再按查询出的记录中的某个字段和日期范围去定位另一页面上的记录.
这种情况下的解决方法是除了从数据表中得的参数外,其它参数要在页面定义成Public的变量,然后在HTML中传这些变量.
NavigateUrl='<%#"QualityLevelQueryDetail.aspx?Employee_Code="+DataBinder.Eval(Container.DataItem,"Employee_Code")+"&strYear="+strYear+"&strMonth="+strMonth%>'>
3:我们在进行页面间的参数传递时,如果参数是中文,按上面的方法进行传递,很多时候接受到的信息中出现乱码。一种解决方法是将 Web.Config文件中的 <globalization >节中编码格式由utf-8改成gb2312。但这种方法可能导致的情况是影响其它页面。另一种解决方法是传递之前对参数进行编码,接收时再进行相应的解码。
编码的函数很简单,直接使用Server.UrlEncode(string)方法,解码Server.UrlDecode(string).Trim().在相应的HTML中,代码如下:
NavigateUrl='<%#"EmpEfficiencyCompareDetail.aspx?Employee_Code="+DataBinder.Eval(Container,"DataItem.Employee_Code")+" &Employee_Name="+ConvertEncode(DataBinder.Eval(Container, "DataItem.Employee_Name").ToString())+" &procedureName="+ConvertEncode(procedureName)+" &dateFrom="+dateFrom+" &dateTo="+dateTo %>'>
<%#"empefficiencycomparedetail.aspx?employee_code="+databinder.eval(container,"dataitem.employee_code")+" &Employee_Name="+ConvertEncode(DataBinder.Eval(Container, "DataItem.Employee_Name").ToString())+" &procedureName="+ConvertEncode(procedureName)+" &dateFrom="+dateFrom+" &dateTo="+dateTo %><%#"empefficiencycomparedetail.aspx?employee_code="+databinder.eval(container,"dataitem.employee_code")+" &Employee_Name="+ConvertEncode(DataBinder.Eval(Container, "DataItem.Employee_Name").ToString())+" &procedureName="+ConvertEncode(procedureName)+" &dateFrom="+dateFrom+" &dateTo="+dateTo %>二:将网格中的数据导入到Excel的一个简单方法(适应于将网格中数据完全不改变的导入到Excel中)
public void ExportToExcel(System.Web.UI.Control ctl)
{
bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true;
Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-cn",true);
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}一般的绑定方法<%# DataBinder.Eval(Container.DataItem, "字段名") %>
用DataBinder.eval 绑定不必关心数据来源(Dataread或dataset)。不必关心数据的类型eval会把这个数据对象转换为一个字符串。在底层绑定做了很多工作,使用了反射性能。正因为使用方便了,但却影响了数据性能。 来看下<%# DataBinder.Eval(Container.DataItem, "字段名") %>。当于dataset绑定时,DataItem其实式一个DataRowView(如果绑定的是一个数据读取器(dataread)它就是一个IdataRecord。)因此直接转换成DataRowView的话,将会给性能带来很大提升。.
<%# ctype(Container.DataItem,DataRowView).Row("字段名") %>
*对数据的绑定建议使用<%# ctype(Container.DataItem,DataRowView).Row("字段名") %>。数据量大的时候可提高几百倍的速度。使用时注意2方面:
1.需在页面添加引用System.Data单元。using System.Data;
2.注意字段名的大小写(要特别注意)。如果和查询的不一致,在某些情况下会导致比<%# DataBinder.Eval(Container.DataItem, "字段名") %>还要慢。如果想进一步提高速度,可采用
<%# ctype(Container.DataItem,DataRowView).Row(0) %>的方法。不过其可读性不高
四:日期格式化
1:C#代码中指定日期显示格式 (注意,格式"yyyy-MM-dd"中,MM一定要大写,大写指的是月份,小写指的是分钟)
DateTime curDate;
curDate = DateTime.Now;
CurDate.Text = curDate.AddDays(-1).ToString("yyyy-MM-dd") 2:HTML文件中指定日期显示格式
<%# DataBinder.Eval(Container.DataItem,"Product_Date","{0:yyyy-MM-dd}")%>
五.获取错误信息并到指定页面
不使用Response.Redirect,而使用Server.Transfer
e.g
// in global.asax
protected void Application_Error(Object sender, EventArgs e) {
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");
//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了 :)
}
Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理
六.DataGrid行随鼠标变色
private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType!=ListItemType.Header)
{
e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"");
e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=\""+ "#EFF3F7"+"\"");
}
}
七DateTime类型
1.绑定时格式化日期方法:
<ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " >
<ITEMSTYLE WIDTH= "18% " > </ITEMSTYLE >
</ASP:BOUNDCOLUMN >
2.数据控件如DataGrid/DataList等的件格式化日期方法:
e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();
3.用String类转换日期显示格式:
String.Format( "yyyy-MM-dd ",yourDateTime);
4.用Convert方法转换日期显示格式:
Convert.ToDateTime("2005-8-23").ToString
("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo); //支持繁体数据库
5.直接用ToString方法转换日期显示格式:
DateTime.Now.ToString("yyyyMMddhhmmss");
DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")
6.只显示年月
DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}")
7.显示时间所有部分,包括:年月日时分秒
<asp:BoundColumn DataField="收款时间" HeaderText="收款时间"
DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}">
</asp:BoundColumn>
八:自定义消息弹出框
public static void Alert(string strMessage,System.Web.UI.Page page)
{
page.RegisterStartupScript("","<script>alert('"+strMessage+"');</script>");
}

浙公网安备 33010602011771号