您的位置:首页 > 编程语言 > ASP

asp.net 记录一下教常用的代码吧 记忆力太差了

2013-06-06 09:53 459 查看
A标签防止中文乱码 
<a href="KSXX_List.aspx?type=<%=Server.UrlEncode("政务管理") %>&id=T1" target="_blank"   class="ablud">政务管理</a>


获取后台的值...经常忘啊

<a href='<%=imgLink[0] %>' .....


调用后台方法

<img alt="" src='<%#getImageUrl(Eval("L_CONTENT").ToString()) %>' width="160" height="120"
border="0" />


 

从字符串中获取图片路径

/// <summary>
/// 从字符串中获取需要的图片地址
/// </summary>
/// <param name="source">字符串</param>
/// <returns>需要的图片地址</returns>
public string getImageUrl(string source)
{
ArrayList list = new ArrayList();
string result = "";
string pattern = "<img[^<>]*?\\ssrc=['\"]?(.*?)['\"].*?>";
Regex reg = new Regex(pattern);
MatchCollection mc = reg.Matches(source);
foreach (Match m in mc)
{
list.Add(m.Groups[1].Value);
}
if (list.Count > 0)
{
string s = list[0].ToString().TrimStart('/');
result = s.Substring(s.IndexOf('/') + 1);
}
else
{
result = "images/noPic.jpg";
}
return result;
}


 

向DataTable不够8行的话,凑足8行数据

DataSet ds = dm.getsql(sql);
//数据量少 加空行
if (ds.Tables[0].Rows.Count < 8)
{
int dtRows = ds.Tables[0].Rows.Count;//现在的行数
int forno = 8 - dtRows;
for (int i = 0; i < forno; i++)
{
DataRow dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
}
}


 DataTable 导出Excel 自定义文件名

protected void btnExportExcel_Click(object sender, EventArgs e)
{

DataTable dt = VSDataSet.Tables[0];
dt.Columns.Remove("WKKID");
DataTable4Excel(dt, "Excel");
}

/// <summary>
/// Datatable to Excel带自定文件名的导出
/// </summary>
/// <param name="dt"></param>
/// <param name="FileName"></param>
private void DataTable4Excel(System.Data.DataTable dt, string FileName)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
DataGrid excel = new DataGrid();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
headerStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
excel.HeaderStyle.MergeWith(headerStyle);
excel.GridLines = GridLines.Both;
excel.HeaderStyle.Font.Bold = true;
excel.DataSource = dt.DefaultView; //输出DataTable的内容
excel.DataBind();
excel.RenderControl(htmlWriter);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename='" + FileName + ".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = ".xls";
HttpContext.Current.Response.Write(stringWriter.ToString());
HttpContext.Current.Response.End();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: