20 statements frequently used by ASP. NET (recommended)

Source: Internet
Author: User

1. // the dialog box is displayed. Click to go to the specified page.
Response. Write (& quot; <script> window. alert ('the application has not been submitted for this member. Please submit it again! ')

</Script> & quot ;);
Response. Write (& quot; <script> window. location

= 'HTTP: // www.51aspx.com/bizpulic/upmeb.aspx' </script> & quot ;);

2. // pop-up dialog box
Response. Write (& quot; <script language = 'javascript '> alert ('product added successfully! ')

</Script> & quot ;);
3. // delete an object
String filename example &quot;20059595157517.jpg & quot ;;
Pub. util. DeleteFile (HttpContext. Current. Server. MapPath (& quot; ../file/& quot ;)

+ Filename );

4. // bind the drop-down list box datalist
System. Data. DataView dv = conn. Exec_ex (& quot; select-1 as code, 'select business Mode'

As content from dealin union select code, content from dealin & quot ;);
This. dealincode. DataSource = dv;
This. dealincode. DataTextField = & quot; content & quot ;;
This. dealincode. DataValueField = & quot; code & quot ;;
This. dealincode. DataBind ();
This. dealincode. Items. FindByValue (dv [0] [& quot; dealincode & quot;]. ToString

(). Selected = true;

5. // display the time in seconds

<% # System. DateTime. Parse (DataBinder. Eval

(Container. DataItem, & quot; begtime & quot;). ToString (). tow.datestring () %>

6. // link to the title

<% # & Quot; <a class =\& quot; 12c \ & quot; target =\& quot; _ blank \ & quot;

Href =\& quot; http: // www.51aspx/CV/_ & quot; + DataBinder. Eval

(Container.dataitem,&quot;procode&quot;{&quot;.html \ & quot;> & quot; +

DataBinder. Eval (Container. DataItem, & quot; proname & quot;) + & quot; </a> & quot; %>

7. // modify the redirection

<% # & Quot; <A href =\& quot; editpushpro. aspx? Id = & quot; + DataBinder. Eval

(Container. DataItem, & quot; code & quot;) + & quot; \ & quot;> & quot; + & quot; modify

& Quot; + & quot; </A> & quot; %>

8. // The OK button is displayed.

<% # & Quot; <A id =\& quot; btnDelete \ & quot; onclick =\& quot; return confirm ('Do you want

Are you sure you want to delete this record? '); \ & Quot; href =\& quot; pushproduct. aspx?

Dl = & quot; + DataBinder. Eval (Container. DataItem, & quot; code & quot ;)

+ & Quot; \ & quot;> & quot; + & quot; Delete & quot; + & quot; </A> & quot; %>

9. // format the output data "{0: F2}". The format F2 indicates that there are two digits after the decimal point.

<% # DataBinder. Eval (Container, & quot; DataItem. PriceMoney & quot;, & quot; {0: F2}

& Quot;) %>

10. // extract dynamic webpage content

Uri uri = new Uri (& quot; http://www.51aspx.com/&quot ;);
WebRequest req = WebRequest. Create (uri );
WebResponse resp = req. GetResponse ();
Stream str = resp. GetResponseStream ();
StreamReader sr = new StreamReader (str, System. Text. Encoding. Default );
String t = sr. ReadToEnd ();
This. Response. Write (t. ToString ());

11. // get the characters After "."

I. ToString (). Trim (). Substring (I. ToString (). Trim (). LastIndexOf (& quot;. & quot ;)

+ 1). ToLower (). Trim ()

12. open a new window and send parameters:
Transfer Parameters:

Response. write (& quot; <script> window. open ('*. aspx?

Id = & quot; + this. DropDownList1.SelectIndex + & quot; & amp; id1 = & quot; +... + & quot; ') <

/Script> & quot ;)

Receiving parameters:

String a = Request. QueryString (& quot; id & quot ;);
String B = Request. QueryString (& quot; id1 & quot ;);

12. Add dialog box for button

Button1.Attributes. Add (& quot; onclick & quot;, & quot; return confirm ('OK? ')

& Quot ;);
Button. attributes. add (& quot; onclick & quot;, & quot; if (confirm ('Are you sure ...?

') {Return true;} else {return false ;}& quot ;)

13. Delete selected table records

Int intEmpID = (int) MyDataGrid. DataKeys [e. Item. ItemIndex];
String deleteCmd = & quot; Delete from Employee where emp_id = & quot; +

IntEmpID. ToString ()

14. Delete table record warning

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 (& quot; onclick & quot;, & quot; return confirm ('

Are you sure you want to delete this message '); & quot ;);
Break;
Default:
Break;
}
}

15. Click the table row link to another page.

Private void grdCustomer_ItemDataBound (object sender,

System. Web. UI. WebControls. DataGridItemEventArgs e)
{
// Click Open Table
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType =

ListItemType. AlternatingItem)
E. Item. Attributes. Add (& quot; onclick & quot;, & quot; window. open ('

Default. aspx? Id = & quot; + e. Item. Cells [0]. Text + & quot; '); & quot ;);
}

Double-click a table to connect to another page
In the itemDataBind event

If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType =

ListItemType. AlternatingItem)
{
String orderItemID = e. item. cells [1]. Text;
E. item. Attributes. Add (& quot; ondblclick & quot ;,

& Quot; location. href = '../ShippedGrid. aspx? Id = & quot; + orderItemID + & quot ;'

& Quot ;);
}

Double-click a table to open a new page.

If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType =

ListItemType. AlternatingItem)
{
String orderItemID = e. item. cells [1]. Text;
E. item. Attributes. Add (& quot; ondblclick & quot;, & quot; open

('../ShippedGrid. aspx? Id = & quot; + orderItemID + & quot; ') & quot ;);
}

16. Table hyperjoin column Transfer Parameters

<Asp: HyperLinkColumn Target = & quot; _ blank & quot; headertext = & quot; Id & quot;

DataTextField = & quot; id & quot; NavigateUrl = & quot; aaa. aspx? Id ='
<% # DataBinder. Eval (Container. DataItem, & quot; data field 1 & quot;) %> '& amp;

Name = '<% # DataBinder. Eval (Container. DataItem, & quot; data field 2 & quot;) %> '/

>
17. Click to change the color of the table.

If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType =

ListItemType. AlternatingItem)
{
E. Item. Attributes. Add

(& Quot; onclick & quot;, & quot; this. style. backgroundColor = '#99cc00 ';
This. style. color = 'buttontext'; this. style. cursor = 'default'; & quot ;);
}

Written in _ ItemDataBound of the DataGrid


If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType =

ListItemType. AlternatingItem)
{
E. Item. Attributes. Add

(& Quot; onmouseover & quot;, & quot; this. style. backgroundColor = '#99cc00 ';
This. style. color = 'buttontext'; this. style. cursor = 'default'; & quot ;);
E. Item. Attributes. Add (& quot; onmouseout & quot;, & quot; this. style. backgroundColor =

''; This. style. color =''; & quot ;);
}

18. About 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 "))
19. 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 void Application_Error (Object sender, EventArgs e ){
If (Server. GetLastError () is HttpUnhandledException)
Server. Transfer (& quot; MyErrorPage. aspx & quot ;);

// The remaining non-HttpUnhandledException exceptions are handled by ASP. NET itself and okay will be handled :)
}
Redirect will cause post-back generation and thus the loss of error information, so the page orientation should be directly in the server

Run the command on the server to obtain the error information and handle the error on the error handling page.
20. Clear cookies

Cookie. Expires = [DateTime];
Response. Cookies (& quot; UserName & quot;). 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.