C # interaction with Java Functions

Source: Internet
Author: User

C # interaction with Java Functions
1. Call the method of jacascript at the front end in the C # code of the background:

Method 1: add the Java function to the Click Event of the button in the page_load event.

HTML code:

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function jstest ()
{
Alert ("this is the js method ");
Return false;
}
</SCRIPT>
1. <asp: button id = "button1" runat = "server" text = "button"/> // Javascript is implemented. The onclientclick/onclick attribute can be added or not.
2. <asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/> // onclick is added, the JS function is executed together with the onclick method in the background.

. CS code:

Protected void page_load (Object sender, eventargs E)
{
Button1.attributes ["onclick"] = "Return jstest ();"; // jstest () is a JS function.
}
Method 2: Call it directly in the click event. HTML code:

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function jstest ()
{
Alert ("this is the js method ");
Return false;
}
</SCRIPT>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/>
. CS code:

Protected void button#click (Object sender, eventargs E)
{
Clientscript. registerstartupscript (this. GetType (), "clear", "<SCRIPT> jstest () </SCRIPT> ");
}
2. Javascript calls the C # method in the front-end html

1). the CS background C # method has a return value. The Code is as follows:

 

Protected string csharpmethod1 (string STRC) // a parameter exists.
{
String text = STRC;
Textbox1.text = text;
Return STRC;
}
Protected string csharpmethod2 () // no difference
{
Textbox1.text = button1.text;
Return textbox1.text. tostring ();
}
HTML code:

 

<Script language = "JavaScript">
Function jstest1 ()
{
VaR S = "<% = csharpvoid (" parameter method available ") %> ";
// Return S;
Document. Write (s );
}
Function jstest2 ()
{
VaR S = "<% = csharpvoid () %> ";
Return S;
}
</SCRIPT>

<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "jstest1 ()"/>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "jstest2 ()"/>
2) C # No return value for the method,. CS code:

Public void csharpvoid ()
{
String text = button1.text;
Textbox1.text = text;
}
HTML code:

<Script language = "JavaScript">
Document. Write ("<% csharpvoid (); %> ");
</SCRIPT>
Another method is to write the event content to the Click Event of a control, and hide the control settings. In this way, only the background method is available and JavaScript is used to call the control. As shown below, there is a button button1 event.

. CS code:

Protected void button#click (Object sender, eventargs E)
{
Textbox1.text = "JS call button method ";
}
Then, JavaScript Functions are called using the client events of button2 in HTML. The code is:

 

<Script language = "JavaScript">
Function jstest3 ()
{
Document. getelementbyid ("button1"). Click ();
}
</SCRIPT>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "button" style = "display: none;" onclick = "button#click"/>
<Asp: button id = "button2" runat = "server" text = "button" onclientclick = "jstest3 ()"/>
3. Access the C # variable in Javascript

Define a variable in. CS code

Public String n = "C # variable ";
At the front-end, use Java to call and output this variable.

<Script language = "JavaScript">
Function getcsharp ()
{
VaR S = "<% = n %>"; // n is the background variable.
Document. Write (s );
}
</SCRIPT>
<Asp: button id = "button1" runat = "server" text = "button" onclientclick = "getcsharp ()"/>
4. Call Java variables in C #

Method 1: 1. the foreground uses static text controls to hide the field and write the JS variable value into it. 2. The background uses request ["ID"] to obtain the value;
Method 2: cookie or session can be used

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/claymore1114/archive/2008/12/03/3436502.aspx

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.