JS calls the method with parameters in CS

Source: Internet
Author: User

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!

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.