Asp.net,c# Five ways to call foreground JavaScript in the background __net

Source: Internet
Author: User
C # Five ways to call foreground JavaScript in the background because the project needs, use the other project team with VC developed components, in the Web background code can not access this component, so had to call the foreground JavaScript through the background to operate this component. Looked up on the internet, found that there are three ways to access the foreground code: the first, OnClientClick (VS2003 does not support this method) <asp:button id= "Button1" runat= "Server" text= "button" O

Nclientclick= "Client_click ()" onclick= "Button1_Click"/> Client_click () is a method of JavaScript. 
The second type, BUTTON1.ATTRIBUTES.ADD ("onclick", "Return Client_click ()"); "Client_click ()" is a foreground method that can be replaced with a generic script such as: Retrun confirm (' OK delete ').
        The third kind, I think the most flexible one, clientscript.registerstartupscript example: StringBuilder sb = new StringBuilder (); Sb.
        Append ("<script language= ' JavaScript ' >"); Sb.
        Append ("Button2_onclick ('" + Serverpath + ")"); Sb.
        Append ("</script>"); Clientscript.registerstartupscript (this. GetType (), "Loadpicscript", sb.

ToString ()); The fourth kind. Write a script with the Response.Write method for example, after you click the button, first manipulate the database, after the display has been completed, you can finally want to call the place to write Response.Write ("<script type= ' Text/javascript '"

>alert ();</script> "); A flaw in this approach is that you cannot invoke custom functions in script files, only internal functions, and specific calls to custom functions can only be response.writE write function definitions, such as Response.Write ("<script type= ' Text/javascript ' >function myfun () {...}")

</script> ");

    The fifth way to add scripts dynamically using the ClientScript class is as follows: Add code where you want to invoke a JavaScript script function, and note that the myfun has already been defined in the script file.

Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>myfun ();</script>");
 This method is more convenient than Response.Write, and you can call the custom function in the script file directly.


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.