Asp. NET programs Common Code summary

Source: Internet
Author: User
<summary>
Name: Isnumberic
Function: Determine whether the input is a number
Parameter: Stringotext: Source text
return value: Booltrue: Yes false: No
</summary>

Publicboolisnumberic (Stringotext)
{
Try
{
Intvar1=convert.toint32 (Otext);
Returntrue;
}
Catch
{
Returnfalse;
}
}

Get the actual length of the string (including Chinese characters)

Get the actual length of the string ostring
Publicintstringlength (stringostring)
{
Byte[]strarray=system.text.encoding.default.getbytes (ostring);
Intres=strarray.length;
Returnres;
}
42. Convert the carriage return to tab

When you hit enter on a control that has a KeyDown event, it becomes Tabpublicvoidtab (System.Web.UI.WebControls.WebControlwebcontrol) {WebControl. Attributes.Add ("onkeydown", "if (event.keycode==13) event.keycode=9");}

43.datagrid paging if an out-of-index appears when deleted

Publicvoidjumppage (SYSTEM.WEB.UI.WEBCONTROLS.DATAGRIDDG) {intint_pageless;//defines the number of pages to jump//if the current page is the last page if (DG. CURRENTPAGEINDEX==DG. PAGECOUNT-1) {//If there is only one page if (DG. currentpageindex==0) {//deleted after page stops on the current page DG. CURRENTPAGEINDEX=DG. PageCount-1;} else{//if the last page has only one record if (DG. ITEMS.COUNT%DG. Pagesize==1) | | Dg. Pagesize==1) {//The page should jump to the previous page after the last record of the last page is deleted int_pageless=2;} else//if the number of records on the last page is greater than 1, the last page will still stop at the current page after the record is deleted {Int_pageless=1;} Dg. CURRENTPAGEINDEX=DG. pagecount-int_pageless;}}}

31. When the file is in a different directory, you need to get the database connection string (if the connection string is placed in Web. config and then initialized in global.asax)

Add the following code in Application_Start: application["ConnStr"]=this.   Context.request.physicalapplicationpath+configurationsettings. appsettings["ConnStr"]. ToString ();

32. Variables. ToString ()
Character conversion to string

12345.ToString ("n");//Generate 12,345.0012345.tostring ("C");//Generate ¥12,345.0012345.tostring ("E");//Generate 1.234500e+00412345. ToString ("F4");//Generate 12345.000012345.ToString ("x");//Generate 3039 (16 binary) 12345.ToString ("P");//Generate 1,234,500.00%

33. Variables. Substring (parameter 1, parameter 2);
Intercept a part of the string, parameter 1 is the left start number, parameter 2 is to intercept several. such as: string S1 = str. Substring (0,2);
34. Log on to other sites on your website: (if your page is nested, because a page can have only one form, then you may be directed to another page to submit the login information)

  
     
   
       
     
      
       
        
           /tr> 
         
        
       
      
     
    
   
  

The name of the text box must be the name of the page you want to login, if the source code can not be used vsniffer look.
Here is the code to get the login information entered by the User:

stringname;name=request.querystring["EmailName"];try{inta=name. IndexOf ("@", 0,name. Length); F_user. Value=name. Substring (0,a); F_domain. Value=name. Substring (A+1,name. length-(a+1)); F_pass. value=request.querystring["PSW"];} catch{Script.alert ("Wrong mailbox!"); Server.Transfer ("index.aspx");}

35. Warning window

<summary>///server-side Popup Alert dialog///</summary>///<paramname= "Str_message" > Tip information, Example: "Cannot be empty!" </param>///<paramname= "page" >page class </param>publicvoidalert (stringstr_message,pagepage) {page. RegisterStartupScript ("", "<script>alert ('" +str_message+ "');</script>");}

36. Reload this warning window to get the focus for a control

<summary>///server-side Popup Alert dialog box, and make the control get focus///</summary>///<paramname= "Str_ctl_name" > Get the Focus control ID value, For example: Txt_name</param>///<paramname= "str_message" > Hint information, example: "Please enter your name!" </param>///<paramname= "page" >page class </param>publicvoidalert (stringstr_ctl_name,stringstr_ message,pagepage) {page. RegisterStartupScript ("", "<script>alert ('" +str_message+ ");d ocument.forms (0)." +str_ctl_name+ ". Focus ();d ocument.forms (0)." +str_ctl_name+ ". Select ();</script>");}

37. Confirmation dialog box

<summary>///server-side Popup Confirm dialog///</summary>///<paramname= "Str_message" > Tip information, Example: "Are you sure you want to delete!" </param>///<paramname= "BTN" > Hide Botton button ID value, for example: Btn_flow</param>///<paramname= "page" > Page class </param>publicvoidconfirm (stringstr_message,stringbtn,pagepage) {page. RegisterStartupScript ("", "<script>if (Confirm ('" +str_message+ "') ==true) {document.forms (0)." +btn+ ". Click ();} </script> ");}

38. Overload confirmation dialog box, click OK to trigger a hidden button event, click Cancel to trigger a hidden button event

<summary>///server-side Popup Confirm dialog box asking the user to prepare to move to those actions, including "OK" and "Cancel" action///</summary>///<paramname= "str_ Message > information, such as: "Successfully added data, click \" OK \ "button to fill in the process, click \" Cancel \ "Modify data" </param>///<paramname= "Btn_redirect_flow" > OK button ID value </param>///<paramname= "btn_redirect_self" > "Cancel" button ID value </param>///<paramname= "page" >page class </param>publicvoidconfirm (Stringstr_message,stringbtn_redirect_flow,stringbtn_redirect_self, pagepage) {page. RegisterStartupScript ("", "<script>if (Confirm ('" +str_message+ "') ==true) {document.forms (0)." +btn_redirect_flow+ ". Click ();} Else{document.forms (0). " +btn_redirect_self+ ". Click ();} </script> ");}

39. Get Focus

<summary>///causes the control to get focus///</summary>///<paramname= "Str_ctl_name" > Get the Focus control ID value, such as: txt_name</ Param>///<paramname= "page" >page class </param>publicvoidgetfocus (stringstr_ctl_name,pagepage) {page. RegisterStartupScript ("", "<script>document.forms (0)." +str_ctl_name+ ". Focus ();d ocument.forms (0)." +str_ctl_name+ ". Select ();</script>");}

40. Subform Return main form

<summary>///Name: redirect///function: Subform return main Form//parameter: url///return value: Empty///</summary>publicvoidredirect (StringUrl , pagepage) {if (session["Ifdefault"]!= (object) "Default") {page. RegisterStartupScript ("", "<script>window.top.document.location.href= '" "+url+" ';</script> ");}}

21. How to set global variables

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

22. 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 the new window <br>

  • 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.