asp.net 開發(fā)的一些常用技巧
你可以在不想被緩存的頁面Page_Load事件中加上如下代碼
Response.Expires = 0;
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.AddHeader("pragma", "no-cache");
Response.CacheControl = "no-cache";
編譯成DLL:
“開始”—“運(yùn)行”—“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導(dǎo)出到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();
}
提示并跳轉(zhuǎn):
ScriptManager.RegisterStartupScript(this.Page, GetType(),
"askAndRederect", "alert('請(qǐng)先登錄!');window.location.href='Index.aspx';", true);
GridView中刪除某一行前詢問:
((LinkButton)e.Row.Cells[4].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('您確認(rèn)要?jiǎng)h除嗎?')");
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'");
//當(dāng)鼠標(biāo)移開時(shí)還原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
asp.net 正則表達(dá)式:
//驗(yàn)證手機(jī)號(hào)
Regex r = new Regex(@"^(13|15|18)\d{9}$");
return r.IsMatch(mobilePhone);
//帶86或者+86的手機(jī)號(hào)
Regex regexMobile = new Regex(@"^((\+86)|(86))?(13|15|18)\d{9}$");
//驗(yàn)證是否為中文
Regex regex = new Regex("^[一-龥]+$");
//Decimal(8,4)類型小數(shù)的驗(yàn)證
Regex rx = new Regex(@"^-?(?:[1-9][0-9]{0,3}|0)(?:\.[0-9]{1,4})?$");
//驗(yàn)證輸入是否為數(shù)字和字母的組合
regex = new Regex("^[一-龥_a-zA-Z0-9]+$");
//驗(yàn)證輸入是否全為數(shù)字
regex = new Regex("^[0-9]+$");
//驗(yàn)證輸入是否全為中文
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})(\]?)$");
//驗(yàn)證網(wǎng)址
regex = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
//電話號(hào)碼
regex = new Regex(@"(\d{3})?\d{8}|(\d{4})(\d{7})");
將Button關(guān)聯(lián)上回車鍵的JS方法:
在網(wǎng)頁中設(shè)置回車鍵的解決方法是使用javascript的document.onkeydown()方法捕捉鍵盤點(diǎn)擊事件,使用event.keyCode來獲取用戶點(diǎn)擊的鍵位。
function document.onkeydown()
{
if(event.keyCode == 13)
{
button.click();//點(diǎn)擊回車鍵調(diào)用button的點(diǎn)擊事件
event.returnValue = false;//取消回車鍵的默認(rèn)操作
}
}
如果button按鈕為服務(wù)器端的按鈕,則更改如下
function document.onkeydown()
{
//使用document.getElementById獲取到按鈕對(duì)象
var button = document.getElementById('<=serverButton.ClientID%>');
if(event.keyCode == 13)
{
button.click();
event.returnValue = false;
}
}
如果按鈕在用戶控件中,上面的方法可以放在用戶控件中使用。
一定要取消回車鍵的默認(rèn)操作,否則默認(rèn)的按鈕還會(huì)在執(zhí)行了button按鈕后繼續(xù)執(zhí)行。
相關(guān)文章
Visual?Studio創(chuàng)建WPF項(xiàng)目
這篇文章介紹了使用Visual?Studio創(chuàng)建WPF項(xiàng)目的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁實(shí)現(xiàn)代碼
今天與大家分享一下“Asp.Net數(shù)據(jù)控件引用AspNetPager.dll分頁”首先聲明以下幾點(diǎn)2012-01-01Web.Config文件配置之限制上傳文件大小和時(shí)間的屬性配置
在Web.Config文件中配置限制上傳文件大小與時(shí)間字符串時(shí),是在httpRuntime httpRuntime節(jié)中完成的,需要設(shè)置以下2個(gè)屬性:maxRequestLength屬性與ExecutionTimeout屬性,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02asp.net下Linq To Sql注意事項(xiàng)小結(jié)
對(duì)于Linq 連接數(shù)據(jù)庫進(jìn)行操作時(shí)需注意的問題2008-10-10asp.net靜態(tài)方法彈出對(duì)話框?qū)崿F(xiàn)思路
為菜鳥所準(zhǔn)備……其實(shí)就是彈出JavaScript小窗口,總得來說就是定義的一個(gè)DIV,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)asp.net有所幫助2013-02-02asp.net+sqlserver實(shí)現(xiàn)的簡單高效的權(quán)限設(shè)計(jì)示例
大部分系統(tǒng)都有權(quán)限系統(tǒng)。一般來說,它能管控人員對(duì)某個(gè)否頁面的訪問;對(duì)某些字段、控件可見或者不可見。對(duì)gridview中的數(shù)據(jù)是否可刪除、可添加、可新增等等。2010-04-04ASP.NET―001:GridView綁定List、頁面返回值具體實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET―GridView綁定List、頁面返回值具體實(shí)現(xiàn),需要的朋友可以參考下2014-02-02Asp.net 通用萬級(jí)數(shù)據(jù)分頁代碼[修正下載地址]
在萬級(jí)數(shù)據(jù)量下的分頁代碼2008-10-10