Simple common ASP. NET code (1) (1)

Source: Internet
Author: User

The following is a summary of the 33 common ASP. NET codes that help beginners learn more about ASP. NET.

ASP. NET Common Code 1. open a new window and send parameters:

Transfer Parameters:

 
 
  1. response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")   

Receiving parameters:

 
 
  1. string a = Request.QueryString("id");   
  2. string b = Request.QueryString("id1");   

Common ASP. NET code 2. Button Addition Dialog Box

 
 
  1. Button1.Attributes. Add ("Onclick","Return confirm ('OK? ')");
  2. Button. attributes. add ("Onclick","If (confirm ('are you sure ...? ') {Return true;} else {return false ;}")
  3.  

ASP. NET Common Code 3. Delete selected table records

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

ASP. NET Common Code 4. Delete table record warning

 
 
  1. Private VoidDataGrid_ItemCreated (Object sender, DataGridItemEventArgs e)
  2. {
  3.  Switch(E. Item. ItemType)
  4. {
  5. CaseListItemType. Item:
  6. CaseListItemType. AlternatingItem:
  7. CaseListItemType. EditItem:
  8. TableCell myTableCell;
  9. MyTableCell = e. Item. Cells [14];
  10. LinkButton myDeleteButton;
  11. MyDeleteButton = (LinkButton) myTableCell. Controls [0];
  12. MyDeleteButton. Attributes. Add ("Onclick","Return confirm ('Are you sure you want to delete this information ');");
  13.  Break;
  14. Default:
  15.  Break;
  16. }
  17.  
  18. }
  19.  

ASP. NET Common Code 5. Click the table row link to another page

 
 
  1. Private VoidGrdCustomer_ItemDataBound (ObjectSender, System. Web. UI. WebControls. DataGridItemEventArgs e)
  2. {
  3.  // Click Open Table 
  4.  If(E. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
  5. E. Item. Attributes. Add ("Onclick","Window. open ('default. aspx? Id ="+ E. Item. Cells [0]. Text +"');");
  6. }
  7.  
  8. Double-click a table to connect to another page
  9.  
  10. In the itemDataBind event
  11.  
  12. If(E. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
  13. {
  14.  StringOrderItemID = e. item. cells [1]. Text;
  15. ...
  16. E. item. Attributes. Add ("Ondblclick","Location. href = '../ShippedGrid. aspx? Id ="+ OrderItemID +"'");
  17. }
  18.  
  19. Double-click a table to open a new page.
  20.  
  21. If(E. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
  22. {
  23.  StringOrderItemID = e. item. cells [1]. Text;
  24. ...
  25. E. item. Attributes. Add ("Ondblclick","Open ('../ShippedGrid. aspx? Id ="+ OrderItemID +"')");
  26. }
  27.  

★Note :? Id =] cannot be? Id =]

Common ASP. NET Code 6. Passing parameters in Table hyperjoin Columns

 
 
  1. <Asp: HyperLinkColumn Target ="_ Blank"Headertext ="ID"DataTextField ="Id"NavigateUrl = "aaa. aspx? Id ='
  2. <% # DataBinder. Eval (Container. DataItem,"Data field 1") %> '& Name =' <% # DataBinder. Eval (Container. DataItem,"Data field 2") %> '/>
  3.  

Common ASP. NET code 7. Change the color of a table by clicking

 
 
  1. if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)   
  2. {   
  3.  e.Item.Attributes.Add("onclick","this.style.backgroundColor=’#99cc00’;   
  4.   this.style.color=’buttontext’;this.style.cursor=’default’;");   
  5. }     
  6.  

Written in _ ItemDataBound of the DataGrid

 
 
  1. if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)   
  2. {   
  3. e.Item.Attributes.Add("onmouseover","this.style.backgroundColor=’#99cc00’;   
  4.  this.style.color=’buttontext’;this.style.cursor=’default’;");   
  5. e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.color=’’;");   
  6. }    
  7.  

Common ASP. NET code 8. Date Format

Date Format setting:

 
 
  1. DataFormatString="{0:yyyy-MM-dd}"    
  2.  

I think it should be in the itembound event

 
 
  1. E. items. cell ["Your column"]. Text = DateTime. Parse (e. items. cell ["Your column"]. Text. ToString ("Yyyy-MM-dd"))

ASP. NET Common Code 9. Get error information and go to the specified page

Instead of using Response. Redirect, you should use Server. Transfer

E. g

 
 
  1. // In global. asax 
  2. Protected VoidApplication_Error (Object sender, EventArgs e ){
  3. If(Server. GetLastError ()IsHttpUnhandledException)
  4. Server. Transfer ("MyErrorPage. aspx");
  5.  
  6. // The remaining non-HttpUnhandledException exceptions are handled by asp.net itself and okay will be handled :) 
  7. }

Redirect will lead to post-back loss of error information, so the page orientation should be executed directly on the server side, so that the error information can be obtained on the error handling page and processed accordingly

10. Clearing cookies in common ASP. NET Codes

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.