09 2012 档案
可空类型的处理
摘要:在程序中经常碰到时间,或是数字类型信息,这些字段在数据库中可以为空,但是时间和数字等值类型都必须要求有值。可空类型顾名思义类型可以为空,它使得值类型可以为空,定义型如className?,例如时间的可空类型为DateTime?。将String类型转换成DateTime? 1 private DateTime? GetNullableDateTime(string str) 2 { 3 return string.IsNullOrEmpty(str) ? null : 4 (DateTime?)Convert.ToDateTime(str)...
阅读全文
字符串扩展方法
摘要:字符串扩展方法 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.Text; 7 8 public static class StringExtend 9 {10 public static DateTime ToDatetime(this string str)11 {12 if (string.IsNullOrEmpty(str))13 return DateTi...
阅读全文
如何从页面中获取url参数
摘要:页面中获取url参数,一便其他的方法调用1>通过属性获取,将url参数封装到属性中统一调用。1 public string Id { get { return Request.QueryString["id"]; } }2>通过方法获取,将url参数封装到方法中,作为方法的结果返回。1 protected string GetUrlId()2 {3 return Request.QueryString["id"];4 }3>通过字段获取,字段一 个字段,在页面的page_load事件中初始化url参数的值给该字段 1 public st
阅读全文
委托事件的实际运用
摘要:1>事件在try catch中的运用在多层架构中try catch事件类似于冒泡,如果不对代码做任何处理,无论哪一层发生异常,都会在最顶层的ul层显示出异常信息。所以个人观点,异常的捕获工作最好在ul层。这样所有层级报的异常都可以被捕获到,因为错误会像气泡一样最终会在显示层暴露出来。在每次设计到数据库的时候捕获异常,这样每个页面的数据操作都会被try catch包裹,有没有一种更好得方法,让代码更简洁。答案是肯定的。将控件事件包裹起来,重新申明一个事件,在该事件中调用控件的事件,并且捕获异常。 1 using System; 2 using System.Collections.Gene
阅读全文
使用NPOI实现excel的导入导出
摘要:NPOI2可以对excle2003和excle2007进行导入导出的操作 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.IO;
阅读全文
文件下载的四种方式
摘要:1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.IO; 7 8 /// 9 /// 文件下载有以下四种方式, 大文件下载的处理方法:将文件分块下载。 10 /// Response.OutputStream.Write 11 /// Response.TransmitFile 12 /// Response.WriteFile 13 /// Response.BinaryWrite 14 /// ...
阅读全文
文件上传通用类
摘要:1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 using System.IO; 7 using System.Web.UI.WebControls; 8 9 public class FileUploadHelper10 {11 private HttpPostedFile postedFile;12 13 public FileUploadHelper(HttpPostedFile postedFile)14 {15...
阅读全文
对象的序列化和反序列化
摘要:将对象序列化成xml字符串,并将xml字符串反序列化成对象 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.IO; 7 using System.Xml; 8 using System.Xml.Serialization; 9 10 namespace XYL.TechnicalSupervision.Utilities11 {12 public class DotNetXmlSerilize13 {14...
阅读全文
treenode中的checkbox添加change事件
摘要:treeview的TreeNodeCheckChanged事件,当checkbox的选择状态改变时,在客户端回发的到服务器的时候会触发该事件。但事实是选择checkbox时没有响应该事情,那是因为treeview控件没有给checkbox添加客户端回发事件。所以问题的症结在于,没有回发事件,解决方案当然是给checkbox添加回发事件。1>在服务器端给treeview添加客户端的onclick事件1 protected void Page_Load(object sender, EventArgs e)2 {3 treeView.Attributes.Add("onclick&
阅读全文
div居中以及div中的元素居中
摘要:div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中的效果。 1. div水平居中:设置margin的左右边距为自动。 2. div中的元素居中 2.1 div中的文字居中:将div的行高设置跟其高度一样的
阅读全文
将html导入到excel或word
摘要:1>通过js实现,在页面的前台调用微软office的com组件,要求用户必须安装office,启动automation服务,并且在浏览器中启用未注册的activex选项.function importToExcel(tableid) { var curTbl = document.getElementById(tableid); try { var oXL = new ActiveXObject("Excel.Application"); } catch (e) { alert("请安装微软Excel并且在浏览器的安全级别中启用‘对未标记为安...
阅读全文