asp.Net開発のいくつかのよく使われるテクニック

4631 ワード

ページキャッシュを使用しない:
キャッシュされたくないページで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」は、次の2つのコマンドでAuthCodeをコンパイルします.csファイルは.DLLファイル.(AuthCode.csファイルは「C:WINDOWSMicrosoft.NETFrameworkv 2.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メソッド:
Webページにリターンキーを設定する解決策は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ボタンが実行された後も実行されます.