Code commonly used in programs

Source: Internet
Author: User

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 ");

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)
{
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 ('Are you sure you want to delete this information ');");
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 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)
{
String orderitemid = 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)
{
String orderitemid = E. Item. cells [1]. text;
...
E. Item. Attributes. Add ("ondblclick", "open ('../shippedgrid. aspx? Id = "+ orderitemid + "')");
}
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 ';");
}
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
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 "))
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 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 for processing and okay.
}
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. 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 applicationexceptionProgramException 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 );
}
}

// Log record type
Using system;
Using system. configuration;
Using system. diagnostics;
Using system. IO;
Using system. text;
Using system. Threading;

Namespace myeventlog
{
/// <Summary>
/// Event logging class, supporting event logging
/// <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 );
}

///


// record the prompt information to the Win2000/NT Event Log
/// text information to be recorded
///
Public static void writeinfo (string message)
{< br> writelog (tracelevel. info, message );
}< BR >///
// record the tracking information to the Win2000/NT Event Log.
/// text information to be recorded
///
Public static void writetrace (string message)
{< br> writelog (tracelevel. verbose, message);
}

///


// format the text information recorded in the event log
/// format required
// exception information title string.
///
/// an exception message string in the format, including the exception content and trace stack.
///
///
Public static string formatexception (exception ex, string catchinfo)
{< br> stringbuilder strbuilder = new stringbuilder ();
If (catchinf O! = String. empty)
{< br> strbuilder. append (catchinfo ). append ("\ r \ n");
}< br> strbuilder. append (ex. message ). append ("\ r \ n "). append (ex. stacktrace);
return strbuilder. tostring ();
}

///


// actual event log Writing Method
/// the level of information to be recorded (error, warning, info, trace ).
// the text to be recorded.
///
Private Static void writelog (tracelevel level, string messagetext)
{< br> try
{< br> eventlogentrytype logentrytype;
switch (level)
{< br> 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 Logs
EventLog. writeentry (messagetext, logentrytype );

}
Catch {} // ignore any exceptions
}
} // Class applicationlog
}

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.