Common ASP. NET development skills 1

Source: Internet
Author: User

A memo prepared by an Asp.net beginner during learning, including "open a new window and send parameters, add a dialog box for the button, and delete the selected records in the table, delete table record warning and other common problems.

1. open a new window and send parameters: Transfer parameter: Response. Write ("<SCRIPT> window. Open ('*. aspx? Id = "+ this. dropdownlist1.selectindex + "& id1 =" +... + "') </SCRIPT>") receiving parameter: string a = request. querystring ("ID"); string B = request. querystring ("id1 "); 2. Add dialog box for button Button1.attributes. Add ("onclick", "Return confirm ('OK? ') "); Button. Attributes. Add (" onclick "," If (confirm ('Are you sure ...? ') {Return true;} else {return false ;}") 3. Delete selected table records Int intempid = (INT) mydatagrid. datakeys [E. Item. itemindex]; string deletecmd = "delete from employee where emp_id =" + intempid. tostring () 4. Delete table record warning Private void datagrid_itemcreated (Object sender, datagriditemeventargs e) {Switch (E. Item. itemtype ){ CA Se 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 ('Are you sure you want to delete this info');"); break; default: break ;}} 5. Click the table row link to another page. Private void grdcustomer_itemdatabound (Object sender, system. web. UI. webcontrols. datagriditemeventargs e) {// click the table to open 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 the 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 ("ondblclick", "location. href = '.. /shippedgrid. aspx? Id = "+ orderitemid +" '");} double-click the 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 ("ondblclick", "open ('.. /shippedgrid. aspx? Id = "+ orderitemid + "')");}★Note :【? Id =] cannot be 【? Id =] 6. Table hyperjoin column Transfer Parameters <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") %> '/> 7. Click to change the color of the table. 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'; ");} is 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 = '';");} 8. About Date Format The date format is set to dataformatstring = "{0: yyyy-mm-dd}" I think E. items. cell ["your column"]. TEXT = datetime. parse (E. items. cell ["your column"]. text. tostring ("yyyy-mm-dd "))
9. Get error information and go to the specified page

Do not use response. redirect, but use server. transfer E. g // In glob al. asaxprotected void application_error (Object sender, eventargs e) {If (server. getlasterror () is httpunhandledexception) server. transfer ("myerrorpage. aspx "); // other non-httpunhandledexception exceptions are handed over to ASP. net's own processing will okay} redirect will lead to post-back generation and thus loss of error information, so the page orientation should be executed directly on the server side, in this way, you can get the error information on the error handling page and process it accordingly. 10. Clear cookies Cookie. expires = [datetime]; response. Cookies ("username"). expires = 0 11. Custom Exception Handling // Custom exception handling class using system; using system. diagnostics; namespace myappexception {// <summary> /// application inherited from the system exception class applicationexception Program Exception Handling class. /// Automatically record the exception content to the application logs of Windows NT/2000 /// </Summary> public class appexception: system. applicationexception {public appexception () {If (applicationconfiguration. eventlogenabled) logevent ("an unknown error occurs. ");} Public appexception (string message) {logevent (Message);} public appexception (string message, exception innerexception) {logevent (Message); If (innerexception! = NULL) {logevent (innerexception. message) ;}}// logging class using system; using system. configuration; using system. diagnostics; using system. io; using system. text; using system. threading; namespace myeventlog {// <summary> // event log record class, event logging support /// <remarks> /// defines four logging methods (error, warning, info, trace) /// </remarks> /// </Summary> public class applicationlog {// <summary> // record the error information to the Win2000/NT Event Log // /<Param name =" Message "> text information to be recorded </param> /// </Summary> Public static void writeerror (string message) {writelog (tracelevel. error, message );} /// <summary> /// record the warning information to the Win2000/NT Event Log /// <Param name = "message"> text information to be recorded </param> /// </Summary> Public static void writewarning (string message) {writelog (tracelevel. warning, message );} /// <summary> /// record the prompt information to the Win2000/NT Event Log /// <Param name = "message"> text information to be recorded </param> /// </summ Ary> Public static void writeinfo (string message) {writelog (tracelevel. info, message );} /// <summary> /// record the tracking information to the Win2000/NT Event Log /// <Param name = "message"> text information to be recorded </param> /// </Summary> Public static void writetrace (string message) {writelog (tracelevel. verbose, message );} /// <summary> /// format the text information recorded in the event log /// <Param name = "ex"> the exception object to be formatted </param> // /<Param name = "catchinfo"> exception information title string. </param> /// <retvalue> /// <Para> formatted exception information string, including the exception content and trace stack. </para> /// </retvalue> /// </Summary> Public static string formatexception (exception ex, string catchinfo) {stringbuilder strbuilder = new stringbuilder (); if (catchinfo! = String. empty) {strbuilder. append (catchinfo ). append ("\ r \ n");} strbuilder. append (ex. message ). append ("\ r \ n "). append (ex. stacktrace); Return strbuilder. tostring () ;}/// <summary> /// actual event log Writing Method /// <Param name = "level"> the level (error, warning, info, trace ). </param> /// <Param name = "messagetext"> the text to be recorded. </param> /// </Summary> Private Static void writelog (tracelevel level, string messagetext) {try {eventlogentrytype logentrytype; Switch (level) {Case tracelevel. error: logentrytype = eventlogentrytype. error; break; Case tracelevel. warning: logentrytype = eventlogentrytype. warning; break; Case tracelevel. info: logentrytype = eventlogentrytype. information; break; Case tracelevel. verbose: logentrytype = eventlogentrytype. successaudit; break; default: logentrytype = eventlogentrytype. successaudit; break;} EventLog = new EventLog ("application", applicationconfiguration. eventlogmachinename, applicationconfiguration. eventlogsourcename); // write Event Log EventLog. writeentry (messagetext, logentrytype);} catch {}// ignore any exceptions} // class applicationlog}
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.