C # code and JavaScript call each other

Source: Internet
Author: User

Question: 1. How do I access C # functions in JavaScript? 2. How do I access C # variables in JavaScript? 3. How do I access the existing variables of JavaScript in C #? 4. How do I access JavaScript functions in C #?

The answer to question 1 is as follows:

To execute functions in C # code in JavaScript functions:

Method One: 1, first establishes a button, in the backstage will call or the processing content writes in the Button_Click;

2, in the foreground to write a JS function, content for document.getElementById ("BTN1"). Click ();

3, in the foreground or backstage call JS function, fire click event, equal access to the background C # function;

Method Two: 1, function declared as public

Background code (change public to protected also can)

public string ss()
{
return("a");
}

2, in the HTML used to call

Foreground script

<script language=javascript>
var a = "<%=ss()%>";
alert(a);
</script>

Method Three: 1,

<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var theForm = document.Form1; //指runat=server的form
theForm.__EVENTTARGET.value = eventTarget;
theFrom.__EVENTARGUMENT.value = eventArgument;

theForm.submit();
}
-->
</script>
<input id="Button1" type="button" name="Button1" value="按钮" onclick="javascript:__doPostBack(’Button1’,’’)">

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.