Java script in ASP. NET calls the c # Method

Source: Internet
Author: User

In ASP. NET development, the background and foreground interactions are often used. This section summarizes the methods for c # and Java script to operate on each other and Java script to call the c # method.
Call the Java script method in the c # code in the background
Java script code:

 
 
  1. <script type="text/javascript" language="javascript"> 
  2. function test()  
  3. {  
  4. alert("oec2003");  
  5. return false;  
  6. }  
  7. </script> 

C # code:

 
 
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3. ClientScript.RegisterStartupScript(this.GetType(), "clear", 
    "<script>test()</script>");  

Java script in ASP. NET calls the c # Method
If the method in c # has a return value, you can use the following method:
C # code

 
 
  1. public string GetAuthStatus()  
  2. {  
  3. ViewState["Auth"] = "Red";  
  4. return ViewState["Auth"].ToString();  

Java script code

 
 
  1. function getAuth()  
  2. {  
  3. var authStatus="<%=GetAuthStatus()%>";  
  4. return authStatus;  

In ASP. in NET, Java script calls the c # method without returning a value. You can put a button on one side and then write what you want to do in the Click Event of the button, write the following code in the client script.

 
 
  1. document.all("button1").click(); 
  1. ASP. NET TypeConverter
  2. Analysis on TypeResolver of ASP. NET
  3. Define JavaScriptConverter in ASP. NET
  4. How to replace Sys. Services in ASP. NET
  5. Use Profile Service of ASP. NET AJAX

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.