asp.net 開發的一些常用技巧

來源:互聯網
上載者:User

不使用頁面緩衝:
你可以在不想被緩衝的頁面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: 複製代碼 代碼如下://驗證手機號
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按鈕後繼續執行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.