Summary of net value of ASP

Source: Internet
Author: User

Article is collected online, I saw that most of the content is suitable for asp.net2.0
1. Open a new window and transfer the parameters:
Transfer parameters:
Response.Write ("<script>window.open (' *.aspx?id=" +this). dropdownlist1.selectindex+ "&id1=" + + "') </script>")
Receive parameters:
String a = request.querystring["id"];
String B = request.querystring["ID1"];

2. Add a dialog box for the button
BUTTON1.ATTRIBUTES.ADD ("onclick", "return confirm (' confirmation? ')");
Button.attributes.add ("onclick", "If" (Confirm (' Is you sure? ')) {return true;} Else{return false;} ")

3. Delete table Selected records
int intempid = (int) Mydatagrid.datakeys[e.item.itemindex];
String deletecmd = "DELETE from Employee where emp_id =" + intempid.tostring ()

4. Delete a 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 message ');");
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 on the form 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 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 +" ') ");
}

★ Special NOTE: "? id=" cannot be "? id ="
6. Table Hyperlink Column Pass Parameters
<asp:hyperlinkcolumn target= "_blank" headertext= "ID number" 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 the _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 formats
Date format setting

Dataformatstring= "{0:YYYY-MM-DD}"
I think it should be in the Itembound incident.
e.items.cell["Your column"].text=datetime.parse (e.items.cell["Your column"].text. ToString ("Yyyy-mm-dd"))

9. Get the error message and go to the specified page
Do not use Response.Redirect, but 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");

The rest of the non-httpunhandledexception exceptions are given to ASP. Okay
}

REDIRECT will cause the Post-back to lose the error message, so the page guidance should be executed directly on the server side, so that the error message can be obtained and processed accordingly.

10. Empty Cookies

Cookie.expires=[datetime];
Response.Cookies ("UserName"). Expires = 0
11. Custom Exception Handling

Custom Exception Handling Classes
Using System;
Using System.Diagnostics;

Namespace Myappexception
{
/**////<summary>
An application exception handling class that inherits from the system exception class ApplicationException.
Automatic logging of exception content to Windows nt/2000 Application log
</summary>
public class AppException:System.ApplicationException
{
Public Appexception ()
{
if (applicationconfiguration.eventlogenabled)
LogEvent ("An unknown error occurred. ");
}

Public appexception (String message)
{
LogEvent (message);
}

Public appexception (String message,exception innerexception)
{
LogEvent (message);
if (innerexception! = null)
{
LogEvent (Innerexception.message);
}
}

Log Record class
Using System;
Using System.Configuration;
Using System.Diagnostics;
Using System.IO;
Using System.Text;
Using System.Threading;

Namespace MyEventLog
{
/**////<summary>
Event logging classes, providing event logging support
<remarks>
4 Logging methods defined (Error, warning, info, trace)
</remarks>
</summary>
public class Applicationlog
{
/**////<summary>
Logging error messages to the Win2000/nt event log
<param name= "message" > Text information required for recording </param>
</summary>
public static void Writeerror (String message)
{

Writelog (tracelevel.error, message);
}

/**////<summary>
Log warning messages to the Win2000/nt event log
<param name= "message" > Text information required for recording </param>
</summary>
public static void Writewarning (String message)
{

Writelog (tracelevel.warning, message);
}

/**////<summary>
Log prompt information to the Win2000/nt event log
<param name= "message" > Text information required for recording </param>
</summary>
public static void Writeinfo (String message)
{
Writelog (tracelevel.info, message);
}
/**////<summary>
Logging trace information to the Win2000/nt event log
<param name= "message" > Text information required for recording </param>
</summary>
public static void Writetrace (String message)
{

Writelog (tracelevel.verbose, message);
}
/**////<summary>
Format text information that is logged to the event log
<param name= "Ex" > Exception object to be formatted </param>
<param name= "Catchinfo" > Exception information Header string .</param>
<retvalue>
<para> exception information string after format, including 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 Write methods
<param name= "Level" > Levels to record information (Error,warning,info,trace) .</param>
<param name= "MessageText" > 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 EventLog = new EventLog ("Application", Applicationconfiguration.eventlogmachinename, Applicationconfiguration.eventlogsourcename);
Write to Event Log
EventLog.WriteEntry (MessageText, Logentrytype);

}
Catch {}//ignore any exceptions
}
}//class Applicationlog
}
12.Panel horizontal scrolling, vertical auto-scaling

<asp:panel style= "Overflow-x:scroll;overflow-y:auto;" ></asp:panel>

13. Enter into tab

<script language= "javascript" for= "document" event= "onkeydown" >
if (event.keycode==13 && event.srcelement.type!= ' button ' &&
event.srcelement.type!= ' Submit ' && event.srcelement.type!= ' reset '
&& event.srcelement.type!= ' && event.srcelement.type!= ' textarea ');
event.keycode=9;
</script>

Onkeydown= "if (event.keycode==13) event.keycode=9"

Http://dotnet.aspx.cc/exam/enter2tab.aspx

14.DataGrid Super Join Column

datanavigateurlfield= "field name" Datanavigateurlformatstring= "http://xx/inc/delete.aspx?id={0}"

15.DataGrid Line Color with mouse

private void Dgzf_itemdatabound
(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (E.item.itemtype!=listitemtype.header)
{
E.item.attributes.add ("onmouseout", "this.style.backgroundcolor=
/"" +e.item.style["Background-color"]+ "/" ");
E.item.attributes.add ("onmouseover", "this.style.backgroundcolor=/" "+" #EFF3F7 "+"/"");
}
}

16. Template columns

<asp:templatecolumn visible= "False" sortexpression= "demo" headertext= "ID" >
<ITEMTEMPLATE>
<asp:label text= ' <%# databinder.eval (Container.DataItem,
"ArticleID")%> ' runat= "server" width= "80%" id= "Lblcolumn"/>
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>

<asp:templatecolumn headertext= "Selected" >
<ITEMTEMPLATE>
<asp:checkbox id= "Chkexport" runat= "Server"/>
</ITEMTEMPLATE>
<EDITITEMTEMPLATE>
<asp:checkbox id= "Chkexporton" runat= "Server" enabled= "true"/>
</EDITITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>

Background code

protected void Checkall_checkedchanged (object sender, System.EventArgs e)
{
Changes the selection of the column to achieve a full or no selection.
CheckBox Chkexport;
if (checkall.checked)
{
foreach (DataGridItem odatagriditem in Mydatagrid.items)
{
Chkexport = (checkbox) Odatagriditem.findcontrol ("Chkexport");
Chkexport.checked = true;
}
}
Else
{
foreach (DataGridItem odatagriditem in Mydatagrid.items)
{
Chkexport = (checkbox) Odatagriditem.findcontrol ("Chkexport");
chkexport.checked = false;
}
}
}
17. Digital formatting

"<% #Container. DataItem (" price ")%> The result is 500.0000, how to format 500.00?"
<% #Container. DataItem ("Price", "{0:¥#,# #0. xx}")%>
int i=123456;
String s=i.tostring ("###,###.00");

18. Date formatting

aspx page: <%# databinder.eval (Container.DataItem, "company_ureg_date")%>
Displayed as: 2004-8-11 19:44:28
I just want to: 2004-8-11 "

<%# DataBinder.Eval (Container.DataItem, "company_ureg_date", "{0:yyyy-m-d}")%>

What should be changed?

"Formatted Date"
Take out, usually an object
((DateTime) objectfromdb). ToString ("Yyyy-mm-dd");

"Validation Expressions for dates"
A. The following correct input format: [2004-2-29], [2004-02-29 10:29:39 PM], [2004/12/31]

^ ((/d{2} (([02468][048]) | ( [13579] [26])) [/-///s]? (((0? [13578]) | (1[02]))
[/-///s]? ((0? [1-9]) | ([1-2][0-9]) | (3[01])) | ((0? [469]) | (11)) [/-///s]? ((0? [1-9]) |
([1-2][0-9]) | (30))) | (0?2[/-///s]? ((0? [1-9]) | ([1-2][0-9])))) | (/d{2} ([02468]
[1235679]) | ([13579][01345789])) [/-///s]? (((0? [13578]) | (1[02])) [/-///S]
? ((0? [1-9]) | ([1-2][0-9]) | (3[01])) | ((0? [469]) | (11)) [/-///s]? ((0? [1-9]) |
([1-2][0-9]) | (30))) | (0?2[/-///s]? ((0? [1-9]) | (1[0-9]) | (2[0-8])))))
(/s ((0?[ 1-9]) | (1[0-2])) /:([0-5][0-9]) ((s) | ( /:([0-5][0-9])/s))
([am| pm|am|pm]{2,2})))? $

B. The following correct input format: [0001-12-31], [9999 09 30], [2002/03/03]

^/D{4}[/-///S]? (((0[13578) | (1[02])) [/-///s]? ([0-2][0-9]) | (3[01])) |
((0[469) | (11)) [/-///s]? ([0-2][0-9]) | (30))) | (02[/-///s]? [0-2] [0-9])) $

"Case Conversion"

Httputility.htmlencode (string);
Httputility.htmldecode (String)

19. How to set global variables

In Global.asax
In the Application_Start () event
Add application[property name] = XXX;
Is your global variable.

20. How do I make a connection to HyperLinkColumn and click Connect to open a new window?

HyperLinkColumn has a property target that sets the value of the device to "_blank". (target= "_blank")

"Aspnetmenu" Click on the menu item to pop up a new window
Add urltarget= "_blank" to the menu item in your Menudata.xml file
Such as:

<?xml version= "1.0" encoding= "GB2312"?>
<menudata imagesbaseurl= "images/" >
<MenuGroup>
<menuitem label= "Reference information" url= "infomation.aspx" >
<menugroup id= "BBC" >
<menuitem label= "announcement information" url= "infomation.aspx"
urltarget= "_blank" lefticon= "file.gif"/>
<menuitem label= "Preparation of information briefs" url= "newinfo.aspx" lefticon= "File.gif"/>

It's a good idea to upgrade your Aspnetmenu to version 1.2.

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.