工作中的新知识
1.有的时候我们会遇到一些可能用到缓存的时候,下面的类就是缓存类,此类涉及缓存的添加、删除、赋值还有提取。
using System;
using System.Collections.Generic; using System.Text;
namespace RCMRS.Common {
///
public class CacheManager {
private static readonly System.Web.Caching.Cache cache; static CacheManager() {
System.Web.HttpContext current = System.Web.HttpContext.Current; if (null != current) {
cache = current.Cache; } else {
cache = System.Web.HttpRuntime.Cache; } }
/// /// 不允许实例化本类的对象 ///
private CacheManager() {
}
/// /// 从指定的键名称中获取缓存对象 ///
/// 缓存的键名称 ///
public static object Get(string key) {
return cache[key]; }
///
/// 缓存的键名称 /// 缓存的值
public static void Set(string key, object value) {
Add(key, value, null); }
///
/// 索引键值 /// 缓存对象
/// 缓存依赖项 public static void Add(string key, object value, System.Web.Caching.CacheDependency dependency) {
cache.Insert(key, value, dependency, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.AboveNormal, null); }
///
/// 索引键值 public static void Remove(string key) {
cache.Remove(key); }
///
public static void Clear() {
System.Collections.IDictionaryEnumerator enumerator = cache.GetEnumerator();
while (enumerator.MoveNext())
{
cache.Remove(enumerator.Key.ToString()); } } } }
2.将 dataset(gridview)导出到excel的两种方法:
A.如果 没有遇到母板页的情况我们可以用以下这种方法。此方法在网上比较好查出来,但是一般不建议用此方法。 ////导出成Word文件
protected void WebToWord() {
Response.Clear();
Response.BufferOutput = true; //设定输出的字符集
Response.Charset = \"GB2312\"; //假定导出的文件名为FileName.doc
Response.AppendHeader(\"Content-Disposition\", \"attachment;filename=StuSubjectiveExam.doc\"); Response.ContentEncoding =
System.Text.Encoding.GetEncoding(\"GB2312\"); ////设置导出文件的格式
Response.ContentType = \"application/ms-word\"; //关闭ViewState
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(\"ZH-CN\", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
GridView4.RenderControl(textWriter); // //把HTML写回浏览器
Response.Write(stringWriter.ToString()); Response.End(); }
注释:此方法必须要在页面中写重写VerifyRenderingInServerForm的函数。 即:
public override void VerifyRenderingInServerForm(Control control) {
//注释掉下面的代码,否则在asp.net2.0下会报错
//base.VerifyRenderingInServerForm(control); }
B.此种方法是用在母板页或者上上述方法出错的情况下:
public bool DoExport(DataSet ds, string[] columns, string fileName) {
if (ds.Tables.Count == 0 || fileName == string.Empty) {
return false; }
Application excel = new ApplicationClass(); int rowindex = 1; int colindex = 0;
Workbook work = excel.Workbooks.Add(true);
//Worksheet sheet1 = (Worksheet)work.Worksheets[0]; System.Data.DataTable table = ds.Tables[0]; if (columns != null) {
for (int i = 0; i < columns.Length; i++) {
colindex++;
if (columns[i] != null && columns[i] != \"\") {
excel.Cells[1, colindex] = columns[i]; } else {
excel.Cells[1, colindex] = table.Columns[i].ColumnName; } } } else {
foreach (DataColumn col in table.Columns) {
colindex++;
excel.Cells[1, colindex] = col.ColumnName; }
}
foreach (DataRow row in table.Rows) {
rowindex++; colindex = 0;
foreach (DataColumn col in table.Columns) {
colindex++;
excel.Cells[rowindex, colindex] = row[col.ColumnName].ToString(); } }
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, XlFileFormat.xlExcel9795, null, null, false, false, XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
excel.Quit(); excel = null; GC.Collect(); return true; }
注释:此方法必须要添加引用Microsoft.Office.Interop.Excel;
3.在repeater(gridview)里添加checkbox复选框,然后得到所选的内容。
A.在repeater里面添加checkbox并获值。 Html页面:
CS文件页面:得到的是所有的选择的内容
foreach (RepeaterItem ri in RepeaterSCCJ.Items) {
CheckBox cb = (CheckBox)ri.FindControl(\"CBxLSCCJ\"); if (cb.Checked) {
string sccjStr =
((System.Web.UI.HtmlControls.HtmlInputHidden)ri.FindControl(\"HiddenSCCJ\")).Value.ToString();
if (sccjStr != \"\") {
CJStr = CJStr + \"'\" + sccjStr + \"'\" + \; }
} }
if (CJStr != \"\") {
CJStr = CJStr.Substring(0, CJStr.Length - 1); }
B. 在gridview里面添加checkbox并获值。 Html页面:
foreach (GridViewRow gvr in GVFenQu.Rows) {
CheckBox cb = (CheckBox)gvr.FindControl(\"CBxFenQu\"); if (cb.Checked) {
string FenQuName =
((System.Web.UI.HtmlControls.HtmlInputHidden)gvr.FindControl(\"HiddenFenQu\")).Value.ToString();
FenQuStr = \"'\" + FenQuName + \"'\" + \ + FenQuStr; } }
if (FenQuStr != \"\") {
FenQuStr = FenQuStr.Substring(0, FenQuStr.Length - 1); }
4..NET统计用量或者别的曲线方法:
Html页面:
<%--曲线图--%>
<%--Series,绘图数据区域,实际呈现的图形形状--%>
Chart1.DataSource = ds.Tables[0];
Chart1.Series[\"Series1\"].XValueMember = \"抄表时间\"; Chart1.Series[\"Series1\"].YValueMembers = \"线损\"; Chart1.DataBind();
System.Web.UI.DataVisualization.Charting.Series series = Chart1.Series[\"Series1\"];
series.ToolTip = \"抄表时间 = #AXISLABEL\\n线损 = #VALY{D}\";
注释:①必须要引用using System.Web.UI.DataVisualization.Charting; 得到一个dataset对象,然后赋值给曲线控件,然后指定列就OK。
②之中学到的一个sql语句:
如果数据库中存在表TABLE则删除
if( object_id('NT1') is not null ) drop table NT1
5.在项目中添加 cookie
添加cookie:
HttpCookie myCookie = new HttpCookie(\"UserSettings\",\"bbbbbbb\"); Response.Cookies.Add(myCookie); 读取cookie:
if (HttpContext.Current.Request.Cookies[\"UserSettings\"] == null) {
Response.Write(\"CookieNonexistence\"); } else
{Response.Write(HttpContext.Current.Request.Cookies[\"UserSettings\"].Value.ToString()); }
因篇幅问题不能全部显示,请点此查看更多更全内容