掃描二維碼關注

首頁 APP開(kāi)發小(xiǎo)程序開(kāi)發 微信公衆号 網站建設 營銷推廣 經典案列 産品服務 關于我們

“學習(xí)不僅是掌握知識”

向書(shū)本學習(xí),還要向實踐學習(xí)、向生活學習(xí)。消化已有(yǒu)知識,
而且要力求有(yǒu)所發現、有(yǒu)所發明(míng)、有(yǒu)所創造

asp.net 開(kāi)發的(de)一些常用(yòng)技巧

2019/4/5 9:28:35

asp.net 開(kāi)發的(de)一些常用(yòng)技巧

不使用(yòng)頁面緩存:
你(nǐ)可(kě)以在不想被緩存的(de)頁面Page_Load事件(jiàn)中加上(shàng)如(rú)下代碼

複制(zhì)代碼 代碼如(rú)下:
Response.Expires = 0;
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
Response.AddHeader("pragma", "no-cache");
Response.CacheControl = "no-cache";

編譯成DLL:
“開(kāi)始”—“運行(xíng)”—“cmd” ,用(yòng)下面的(de)兩條命令編譯AuthCode.cs文(wén)件(jiàn)爲.DLL文(wén)件(jiàn)。(AuthCode.cs文(wén)件(jiàn)保存在“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”目錄下)命令如(rú)下:
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
csc /target:library AuthCode.cs
GridView導出到(dào)Excel代碼2:

複制(zhì)代碼 代碼如(rú)下:
public override void VerifyRenderingInServerForm(Control control)
{
//(必須有(yǒu))
//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();
}

提示并跳轉:

複制(zhì)代碼 代碼如(rú)下:
ScriptManager.RegisterStartupScript(this.Page, GetType(),
"askAndRederect", "alert('請先登錄!');window.location.href='Index.aspx';", true);

GridView中删除某一行(xíng)前詢問:

複制(zhì)代碼 代碼如(rú)下:
((LinkButton)e.Row.Cells[4].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('您确認要删除嗎?')");

GridView隔行(xíng)變色代碼:

複制(zhì)代碼 代碼如(rú)下:
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'");
//當鼠标移開(kāi)時還原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}

asp.net 正則表達式:

複制(zhì)代碼 代碼如(rú)下:
//驗證手機号
Regex r = new Regex(@"^(13|15|18)\d{9}$");
return r.IsMatch(mobilePhone);
//帶86或者+86的(de)手機号
Regex regexMobile = new Regex(@"^((\+86)|(86))?(13|15|18)\d{9}$");
//驗證是否爲中文(wén)
Regex regex = new Regex("^[一-龥]+$");
//Decimal(8,4)類型小(xiǎo)數的(de)驗證
Regex rx = new Regex(@"^-?(?:[1-9][0-9]{0,3}|0)(?:\.[0-9]{1,4})?$");
//驗證輸入是否爲數字和(hé)字母的(de)組合
regex = new Regex("^[一-龥_a-zA-Z0-9]+$");
//驗證輸入是否全爲數字
regex = new Regex("^[0-9]+$");
//驗證輸入是否全爲中文(wén)
regex = new Regex("^[一-龥]+$");
//電子郵件(jiàn)
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- ./?%&=]*)?");
//電話(huà)号碼
regex = new Regex(@"(\d{3})?\d{8}|(\d{4})(\d{7})");

将Button關聯上(shàng)回車(chē)鍵的(de)JS方法:
在網頁中設置回車(chē)鍵的(de)解決方法是使用(yòng)javascript的(de)document.onkeydown()方法捕捉鍵盤點擊事件(jiàn),使用(yòng)event.keyCode來獲取用(yòng)戶點擊的(de)鍵位。

複制(zhì)代碼 代碼如(rú)下:
function document.onkeydown()
{
if(event.keyCode == 13)
{
button.click();//點擊回車(chē)鍵調用(yòng)button的(de)點擊事件(jiàn)
event.returnValue = false;//取消回車(chē)鍵的(de)默認操作
}
}

如(rú)果button按鈕爲服務器(qì)端的(de)按鈕源碼天空,則更改如(rú)下

複制(zhì)代碼 代碼如(rú)下:
function document.onkeydown()
{
//使用(yòng)document.getElementById獲取到(dào)按鈕對象
var button = document.getElementById('<=serverButton.ClientID%>');
if(event.keyCode == 13)
{
button.click();
event.returnValue = false;
}
}

如(rú)果按鈕在用(yòng)戶控件(jiàn)中,上(shàng)面的(de)方法可(kě)以放(fàng)在用(yòng)戶控件(jiàn)中使用(yòng)。
一定要取消回車(chē)鍵的(de)默認操作,否則默認的(de)按鈕還會(huì)在執行(xíng)了button按鈕後繼續執行(xíng)。
 


深圳市南山區南山街(jiē)道南海(hǎi)大(dà)道西(xī)桂廟路(lù)北陽光(guāng)華藝大(dà)廈1棟4F、4G-04

咨詢電話(huà):136 8237 6272
大(dà)客戶咨詢:139 0290 5075
業(yè)務QQ:195006118
技術(shù)QQ:179981967

更多可(kě)以了解的(de)信息

客戶案列
新聞資訊
資質榮譽
團隊風采
項目進度查詢

售前QQ咨詢
QQ溝通 項目QQ溝通

精銳軟件(jiàn)

Copyright© 2018-2023 深圳市無窮大軟件技術有限公司 All Rights Reserved. 京ICP證000000号 公安備案号:粵公網安備44030502009460号