JavaScript calls ASP .:
Method One :
1, first set up a button, in the background will be called or processed content written in Button_Click;
2, write a JS function in the foreground, the content is document.getElementById ("Btn1"). Click ();
3, in the foreground or background call JS function, fire click event, equal access to the background C # function;
Method Two :
1. function declaration is public
Background code (change public to protected also can)
public string methodname ()//Note that the method cannot be void, otherwise execution will cause an error
{
Before this can do any service-side operation, you can not put the return value as the purpose, but to execute some service-side code.
Return "";
}
2, in the HTML with <%=fucntion ()%> can call
Foreground script
<script language=javascript>
var a = "<%=methodname ()%>";
alert (a);
Eval ("<%=methodname ()%>"); If you just want to execute the service side of some code can also be written as follows, so that you can execute the service-side code
</script>
Method Three : Using Pagemethods to invoke background code
Pagemethod Method Description:
Pagemethods.functionname (Paramter1,parameter2,..., successmethod, Failedmethod, UserContext);
Where the preceding Paramter1,parameter2,..., represents the functionname parameter, the type is object or array;
Successmethod is the JS method that needs to use the background return result
Failedmethod is the JS method (a fault-tolerant approach) that executes when the Csfunctionname method in the background has an exception.
UserContext is any content that can be passed to the Successmethod method, or Failedmethod method.
Implementation method Three follow these steps:
1. Create a method in the background, must be static, the method must be of the public type, or access does not report an exception,
Next, add [System.Web.Services.WebMethod] on the head of the method to annotate the method properties.
2. Add the ScriptManager control to the foreground page and set its Enablepagemethods property to True.
3. Call Pagemethods, because the method has a lot of overloads, now only say the simplest implementation.
Pagemethods.functionname (The JS method of callback); where functionname is created for the background static method name, callback JS method for the foreground to accept the results of the method.
Pagemethods Example:
Background code:
I. No-parameter method
[System.Web.Services.WebMethod]
public static string Showvalue ()
{
Return "JS Invoke Background method";
}
Two. Parametric method
[System.Web.Services.WebMethod]
public static string ShowValue2 (String msg)
{
return msg;
}
Front-End Code:
<script type= "Text/javascript" >
Invoke Background parameterless method
function bclick ()
{
Pagemethods.showvalue (sshow);
}
Function Sshow (val)//callback method using Val to accept the execution result of the background code Showvalue
{
document.getElementById ("Show"). InnerText = val;
}
Invoking a parameter method in the background
function Bclick2 ()
{
var text = "Test";
Pagemethods.showvalue2 (TEXT,SSHOW2);
}
Function Sshow2 (val)//callback method using Val to accept the execution result of the background code Showvalue
{
document.getElementById ("Show"). InnerText = val;
}
</script>
<input id= "Button1" type= "button" value= "click" onclick= "bclick ();"/>
<input id= "Button2" type= "button" value= "Click2" onclick= "Bclick2 ();"/>
<div id= "Show" ></div>
JS Call back Code