Js calls many methods in CS. I use a simple method as follows:
CS
Public string test ()
{
Return "Hello World ";
}
Public string test ()
{
Return "Hello World ";
}
Aspx page
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Mce: script type = "text/javascript"> <! --
Var demo = function (){
Var B = "<% = test () %> ";
Alert (B );
}
// --> </Mce: script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "button" id = "id1" onclick = "demo ()" value = "JS call CS"/>
</Div>
</Form>
</Body>
</Html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Mce: script type = "text/javascript"> <! --
Var demo = function (){
Var B = "<% = test () %> ";
Alert (B );
}
// --> </Mce: script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "button" id = "id1" onclick = "demo ()" value = "JS call CS"/>
</Div>
</Form>
</Body>
</Html>
The above does not contain parameters. If the method in the backend CS contains parameters, you must pay attention to it ..
CS:
Public string test (string)
{
Return;
}
[C-sharp] view plaincopy
Public string test (string)
{
Return;
}
Aspx:
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Mce: script type = "text/javascript"> <! --
Var demo = function (){
Var a = "Hello World ";
Var B = '<% = test ("' + a + '") %>'; // pay attention to the use of single and double quotation marks !!!!!
Alert (B );
}
// --> </Mce: script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "button" id = "id1" onclick = "demo ()" value = "JS call CS"/>
</Div>
</Form>
</Body>
</Html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Mce: script type = "text/javascript"> <! --
Var demo = function (){
Var a = "Hello World ";
Var B = '<% = test ("' + a + '") %>'; // pay attention to the use of single and double quotation marks !!!!!
Alert (B );
}
// --> </Mce: script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "button" id = "id1" onclick = "demo ()" value = "JS call CS"/>
</Div>
</Form>
</Body>
</Html>
'<% = Test ("' + a + '") %>' must be enclosed in single quotes '<% = %>', which is then double quotation marks. This is the key. Otherwise
Call failed!