asp.Net常用コード

22092 ワード

1.新しいウィンドウを開き、パラメータを転送します.



  
    
// response.write( " <script>window.open(’*.aspx?id= " + this .DropDownList1.SelectIndex + " &id1= " ++ " ’)</script> " )    // string a = Request.QueryString( " id " ); string b = Request.QueryString( " id1 " );

2.ボタンにダイアログを追加



  
    
Button1.Attributes.Add( " onclick " , " return confirm(’ ?’) " ); button.attributes.add( " onclick " , " if(confirm(’are you sure?’)){return true;}else{return false;} " )

3.テーブル選択レコードの削除



  
    
int intEmpID = ( int )MyDataGrid.DataKeys[e.Item.ItemIndex]; string deleteCmd = " DELETE from Employee where emp_id = " + intEmpID.ToString()

4.テーブルレコード警告の削除



  
    
private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e) {   switch (e.Item.ItemType)  {    case ListItemType.Item :    case ListItemType.AlternatingItem :    case ListItemType.EditItem:    TableCell myTableCell;    myTableCell = e.Item.Cells[ 14 ];    LinkButton myDeleteButton ;    myDeleteButton = (LinkButton)myTableCell.Controls[ 0 ];    myDeleteButton.Attributes.Add( " onclick " , " return confirm(’ ’); " );     break ;    default :     break ;  } }

5.表の行をクリックして別のページにリンクする



  
    
private void grdCustomer_ItemDataBound( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {   //   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   e.Item.Attributes.Add( " onclick " , " window.open(’Default.aspx?id= " + e.Item.Cells[ 0 ].Text + " ’); " ); }    //    // itemDataBind if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {   string OrderItemID = e.item.cells[ 1 ].Text;    e.item.Attributes.Add( " ondblclick " , " location.href=’../ShippedGrid.aspx?id= " + OrderItemID + " " ); } // if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {   string OrderItemID = e.item.cells[ 1 ].Text;    e.item.Attributes.Add( " ondblclick " , " open(’../ShippedGrid.aspx?id= " + OrderItemID + " ’) " ); }   ★ :【 ? id = 】 【 ? id =

6.表のハイパーリンク列伝達パラメータ



  
    
<asp:HyperLinkColumn Target = " _blank " headertext = " ID " DataTextField = " id " NavigateUrl = " aaa.aspx?id=’  < % # DataBinder.Eval(Container.DataItem, " 1 " ) % >’ & name = ’< % # DataBinder.Eval(Container.DataItem, " 2 " ) % >’ /

7.表クリックで色を変える



  
    
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {  e.Item.Attributes.Add( " onclick " , " this.style.backgroundColor=’#99cc00’;     this .style.color = ’buttontext’; this .style.cursor = default ’; " ); }    DataGrid _ItemDataBound if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { e.Item.Attributes.Add( " onmouseover " , " this.style.backgroundColor=’#99cc00’;     this .style.color = ’buttontext’; this .style.cursor = default ’; " ); e.Item.Attributes.Add( " onmouseout " , " this.style.backgroundColor=’’;this.style.color=’’; " ); }

8.日付フォーマットについて



  
    
DataFormatString = " {0:yyyy-MM-dd} "    // itembound e.items.cell[ " " ].text = DateTime.Parse(e.items.cell[ " " ].text.ToString( " yyyy-MM-dd " ))

9.エラー情報を取得して指定ページへ



  
    
// Response.Redirect, Server.Transfer   e.g // in global.asax protected void Application_Error(Object sender, EventArgs e) { if (Server.GetLastError() is HttpUnhandledException) Server.Transfer( " MyErrorPage.aspx " ); // HttpUnhandledException ASP.NET okay :) }    // Redirect post-back , ,

10.クリアクッキー



  
    
Cookie.Expires = [DateTime]; Response.Cookies( " UserName " ).Expires = 0