当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 开发的一些常用技巧

ASP.NET
blog程序新版本V2.0 Beta完成,提供V1.0全部源码下载
“/”应用程序中的服务器错误
asp.net(c#)复数类(复数加减乘除四则运算)
asp.net(c#)不可访问,因为它受保护级别限制
asp.net(c#) 水仙花数
ASP.NET实现用图片进度条显示投票结果
asp.net(c#) MS AJAX的安装
asp.net(c#)有关 Session 操作的几个误区
ASP.net基础知识之常见错误分析
[.net] 操纵自如-页面内的配合与通信
c# static的全部用法收集整理
使CheckBoxList的Attributes属性生效(修改微软的一个bug)
在ASP.NET中使用Session常见问题集锦
[原创]完美解决Could not load file or assembly ''AjaxPro.2'' or one of its dependencies. 拒绝访问。
在Apache环境下成功的运行ASP.NET的注意事项
ClickOnce DIY全自动更新下载升级的自我实现
asp.net jscript 一句话木马
在ASP.Net中实现flv视频转换的代码
将DataTable中的一行复制到另一个DataTable的方法
在DataTable中执行Select("条件")后,返回DataTable的方法

ASP.NET 中的 asp.net 开发的一些常用技巧


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 63 ::
收藏到网摘: n/a

asp.net开发过程中,发现的一些不错技巧,能用的到的朋友有福了。 不使用页面缓存:
你可以在不想被缓存的页面Page_Load事件中加上如下代码
复制代码 代码如下:

Response.Expires = 0;
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.AddHeader("pragma", "no-cache");
Response.CacheControl = "no-cache";

编译成DLL:
“开始”—“运行”—“cmd” ,用下面的两条命令编译AuthCode.cs文件为.DLL文件。(AuthCode.cs文件保存在“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”目录下)命令如下:
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
csc /target:library AuthCode.cs
GridView导出到Excel代码2:
复制代码 代码如下:

public override void VerifyRenderingInServerForm(Control control)
{
//(必须有)
//base.VerifyRenderingInServerForm(control);
}
public void Generate(string Typename, string scswId, GridView TempGrid)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "utf-8";
string Filename = Typename + scswId + ".xls";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "online;filename=" + Filename);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
TempGrid.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Write(oStringWriter.ToString());
HttpContext.Current.Response.End();
}

提示并跳转:
复制代码 代码如下:

ScriptManager.RegisterStartupScript(this.Page, GetType(),
"askAndRederect", "alert('请先登录!');window.location.href='Index.aspx';", true);

GridView中删除某一行前询问:
复制代码 代码如下:

((LinkButton)e.Row.Cells[4].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('您确认要删除吗?')");

GridView隔行变色代码:
复制代码 代码如下:

protected void GVLinkman_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}

asp.net 正则表达式:
复制代码 代码如下:

//验证手机号
Regex r = new Regex(@"^(13|15|18)\d{9}$");
return r.IsMatch(mobilePhone);
//带86或者+86的手机号
Regex regexMobile = new Regex(@"^((\+86)|(86))?(13|15|18)\d{9}$");
//验证是否为中文
Regex regex = new Regex("^[一-龥]+$");
//Decimal(8,4)类型小数的验证
Regex rx = new Regex(@"^-?(?:[1-9][0-9]{0,3}|0)(?:\.[0-9]{1,4})?$");
//验证输入是否为数字和字母的组合
regex = new Regex("^[一-龥_a-zA-Z0-9]+$");
//验证输入是否全为数字
regex = new Regex("^[0-9]+$");
//验证输入是否全为中文
regex = new Regex("^[一-龥]+$");
//电子邮件
regex=new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
//验证网址
regex = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
//电话号码
regex = new Regex(@"(\d{3})?\d{8}|(\d{4})(\d{7})");

将Button关联上回车键的JS方法:
在网页中设置回车键的解决方法是使用javascript的document.onkeydown()方法捕捉键盘点击事件,使用event.keyCode来获取用户点击的键位。
复制代码 代码如下:

function document.onkeydown()
{
if(event.keyCode == 13)
{
button.click();//点击回车键调用button的点击事件
event.returnValue = false;//取消回车键的默认操作
}
}

如果button按钮为服务器端的按钮,则更改如下
复制代码 代码如下:

function document.onkeydown()
{
//使用document.getElementById获取到按钮对象
var button = document.getElementById('<=serverButton.ClientID%>');
if(event.keyCode == 13)
{
button.click();
event.returnValue = false;
}
}

如果按钮在用户控件中,上面的方法可以放在用户控件中使用。
一定要取消回车键的默认操作,否则默认的按钮还会在执行了button按钮后继续执行。