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:
- response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+...+"’)</script>")
Receiving parameters:
- string a = Request.QueryString("id");
- string b = Request.QueryString("id1");
Common ASP. NET code 2. Button Addition Dialog Box
- Button1.Attributes. Add ("Onclick","Return confirm ('OK? ')");
- Button. attributes. add ("Onclick","If (confirm ('are you sure ...? ') {Return true;} else {return false ;}")
-
ASP. NET Common Code 3. Delete selected table records
- int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
- string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()
ASP. NET Common Code 4. Delete table record warning
- Private VoidDataGrid_ItemCreated (Object sender, DataGridItemEventArgs e)
- {
- Switch(E. Item. ItemType)
- {
- CaseListItemType. Item:
- CaseListItemType. AlternatingItem:
- CaseListItemType. EditItem:
- TableCell myTableCell;
- MyTableCell = e. Item. Cells [14];
- LinkButton myDeleteButton;
- MyDeleteButton = (LinkButton) myTableCell. Controls [0];
- MyDeleteButton. Attributes. Add ("Onclick","Return confirm ('Are you sure you want to delete this information ');");
- Break;
- Default:
- Break;
- }
-
- }
-
ASP. NET Common Code 5. Click the table row link to another page
- Private VoidGrdCustomer_ItemDataBound (ObjectSender, System. Web. UI. WebControls. DataGridItemEventArgs e)
- {
- // Click Open Table
- 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 +"');");
- }
-
- Double-click a table to connect to another page
-
- In the itemDataBind event
-
- If(E. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
- {
- StringOrderItemID = e. item. cells [1]. Text;
- ...
- E. item. Attributes. Add ("Ondblclick","Location. href = '../ShippedGrid. aspx? Id ="+ OrderItemID +"'");
- }
-
- Double-click a table to open a new page.
-
- If(E. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
- {
- StringOrderItemID = e. item. cells [1]. Text;
- ...
- E. item. Attributes. Add ("Ondblclick","Open ('../ShippedGrid. aspx? Id ="+ OrderItemID +"')");
- }
-
★Note :? Id =] cannot be? Id =]
Common ASP. NET Code 6. Passing parameters in Table hyperjoin Columns
- <Asp: HyperLinkColumn Target ="_ Blank"Headertext ="ID"DataTextField ="Id"NavigateUrl = "aaa. aspx? Id ='
- <% # DataBinder. Eval (Container. DataItem,"Data field 1") %> '& Name =' <% # DataBinder. Eval (Container. DataItem,"Data field 2") %> '/>
-
Common ASP. NET code 7. Change the color of a table by clicking
- 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’;");
- }
-
Written in _ ItemDataBound of the DataGrid
- 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=’’;");
- }
-
Common ASP. NET code 8. Date Format
Date Format setting:
- DataFormatString="{0:yyyy-MM-dd}"
-
I think it should be in the itembound event
- 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
- // In global. asax
- Protected VoidApplication_Error (Object sender, EventArgs e ){
- If(Server. GetLastError ()IsHttpUnhandledException)
- Server. Transfer ("MyErrorPage. aspx");
-
- // The remaining non-HttpUnhandledException exceptions are handled by asp.net itself and okay will be handled :)
- }
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
- Cookie.Expires=[DateTime];
- Response.Cookies("UserName").Expires = 0