.net开发中vs后台常用的功能实现代码收集

asp.net 欲思 0评论

1.弹出对话框

Response.Write("<script language='javascript'>alert('产品添加成功!')</script >");

2.弹出对话框.点击转向指定页面

Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script>");
Response.Write("<script>window.location ='http://www.msproject.cn/index.asp'</script>");

3.前台验证

<script language="javascript" type="text/javascript">
function check()
{
if(document.getElementById('<%=user_name.ClientID%>').value=="")
{
alert('回复人不能为空!');
return false;
}
return true;
}
</script>

4.//关闭按钮,刷新一页

<a href="/admin/login.aspx" onclick="javascript:window.open('/admin/login.aspx','winopen','width=300,height=161');return false">
<img src="/img/common/admin.gif" width="34" height="18" border="0" /></a>
Response.Write("<script>window.opener.location.replace(opener.location);window.close();</script>");

5.时间去秒显示

<%# System.DateTime.Parse(DataBinder.Eval(Container.DataItem,"begtime").ToString()).ToShortDateString()%>

6.删除弹出确定按钮

<a id="btnDelete" onclick="return confirm('你是否确定删除这条记录吗?');" href="#">删除</a>

7.输出数据格式化 “{0:F2}” 是格式 F2表示小数点后剩两位

<%# DataBinder.Eval(Container, "DataItem.PriceMoney","{0:F2}") %>

8.获取” . “后面的字符

i.ToString().Trim().Substring(i.ToString().Trim().LastIndexOf(".")+1).ToLower().Trim();

9.回车转换成Tab

<script language="javascript" for="document" event="onkeydown">
if(event.keyCode==13 && event.srcElement.type!=’button’ && event.srcElement.type!=’submit’ && event.srcElement.type!=’reset’ && event.srcElement.type!=’’&& event.srcElement.type!=’textarea’)
event.keyCode=9;
</script>

10.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"+""");
}
}

11.数字格式化

【<%#Container.DataItem("price")%>的结果是500.0000,怎样格式化为500.00?】
<%#Container.DataItem("price","{0:¥#,##0.00}")%>
int i=123456;
string s=i.ToString("###,###.00");  //生成 123,456.00
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
string.Format("{0:yyyy-MM-dd}",Convert.ToDateTime(dr["BOOKING_DATE"].ToString()));  //2009-09-26
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?

Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);   //yyyymm等可以设置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);

12.大小写转换

HttpUtility.HtmlEncode(string);
HttpUtility.HtmlDecode(string);

13.GridView三种取值情况

* <1>ReadOnly = true;  this.GridView1.Rows[e.RowIndex].Cells[0].Text
* <2>TextBox           ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
* <3>模板列            this.GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtTitle");

14.截取标题

public string SubStringDescription(string description)
{
if (description.Length > 8)
description = description.Substring(0, 8) + "....";
return description;
}

15.下载
//TransmitFile实现下载

protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/aaa.zip");
Response.TransmitFile(filename);
}

//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{
string fileName ="aaa.zip";//客户端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.zip");//路径

FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

//WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{

string fileName = "aaa.zip";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];

Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
}

//流方式下载
protected void Button4_Click(object sender, EventArgs e)
{
string fileName = "aaa.zip";//客户端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路径

//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

}

iframe滚动条颜色


<style  type="text/css">
BODY
{
ScrollBar-arrow-color:滚动条上下三角按钮
ScrollBar-3d-light-color:立体滚动条亮边的颜色
ScrollBar-base-color:基本颜色
ScrollBar-face-color:凸出部分颜色
ScrollBar-dark-shadow-color:强阴影的颜色
ScrollBar-light-color:空白部分的颜色
ScrollBar-shadow-color:阴影的颜色
}
</style>

但事实上,iframe滚动条颜色根本不会发生改变,为什么呢,是代码最上面的<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>这句内嵌代码在作怪,把这句代码删除,刷新页面,iframe滚动条颜色发生变化,但是新的问题出现了,原来定义在body中的字体大小定义不了,怎么办呢。很简单,把字体大小定义在table中就可以了。]


link:连接平常的状态
active:连接被按下的时候
visited:连接被访问过之后
hover:鼠标放到连接上的时候

转载请注明:欲思博客 » .net开发中vs后台常用的功能实现代码收集

如果你觉得这篇文章或者我分享的主题对你有帮助,请支持我继续更新网站和主题 !捐赠本站
喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址