The various interactions between C # and JS

Source: Internet
Author: User

Today encountered a problem, find good information, put up a record, in case you forget the address, is my notes it!

Many people are working with functions that invoke clients on the server side, which is called scripting functions already defined in the JavaScript script in ASP. After studying, some reluctant methods have been found.


1. Writing scripts using the Response.Write method

For example, after you click the button, the database is finished, the display is finished, you can write in the last place to call
Response.Write ("<script type= ' Text/javascript ' >alert ();</script>");

One drawback of this approach is that you cannot invoke a custom function in a script file, only call an intrinsic function, and call a custom function to write function definitions only in Response.Write, such as Response.Write ("<script type= ' text/ JavaScript ' >function myfun () {...} </script> ");

2. Dynamically add scripts with the ClientScript class


Use the following: Add code where you want to invoke a JavaScript script function, and be sure that Myfun has already been defined in the script file.

Clientscript.registerstartupscript (Clientscript.gettype (), "MyScript", "<script>myfun ();</script>");


This method is more convenient than Response.Write, you can directly invoke the custom function in the script file.


3. Attributes properties for normal add controls

For the normal button is: Button1.Attributes.Add ("onclick", "myfun ();");

Only valid if added in onload or in the initialization process similar to onload. The script function is executed first, and the execution order cannot be changed.


Note that all of the above methods, the background code can not be converted to the current page of the code, such as redirect, to put the paging code in the script


Ask:


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 #?


Question 1 answers are as follows:


The functions in the C # code are executed in the JavaScript function:


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.getelementbyidx ("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 SS ()

{


Return ("a");


}


2, in the HTML with <%=fucntion ()% > can be called


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;
Refers to the form of runat=server


Theform.__eventtarget.value = Eventtarget;


Thefrom.__eventargument.value = eventargument;


Theform.submit ();

}

-->

</script >


<input id= "Button1" type= "button" Name= "Button1" value= "buttons" >


Method Four: <script language= "JavaScript" >


function Submitkeyclick ()

{

if (Event.keycode = = 13)

{

Event.cancelbubble = true;

Event.returnvalue = false;

Document.all.funname.value= "The name of the function you want to invoke";


Document.form[0].submit ();


}


}


</script >


<input id= "AAA" type= "text" >


<input type= "hidden" name= "Funname" >〈! --Used to store the function you want to call--〉

In. cs There are:


Public Page_onload ()

{

if (! Page.ispost ())


{

String strfunname=request.form["Funname"]!=null? request.form["Funname"]: "";


Determines which function to call based on the value returned


Switch (strfunname)

{


Case "Enter ()":


Enter (); Call this function


Break


Case "Other":


Calling other functions

Break

Default

Calling the default function

Break

}

}

}


public void Enter ()

{

...... such as calculating a value

}

==========================================================

Question 2: How do I access C # variables in JavaScript?


The answers are as follows:


Method One: 1, through the page hidden domain access <input id= "xx" type= "hidden" runat= "Server" >


Method Two: 1, such as the background defines the public STRING N; The format for referencing the variable in the foreground JS is ' <%=n% > ' or ' + <%=n% >+ '


Method Three: 1, or you can register a script on the page after the server-side variable is assigned a value

"<script language= ' JavaScript ' >var temp=" + tmp + "</script >"


TMP is a background variable, and then JS can directly access the temp to get the value.

3. How do I access the existing variables of JavaScript in C #?

The answers are as follows:


Method One: 1. The foreground uses the static text control to hide the field and writes the value of the JS variable to it;

2, backstage with request["id" to get the value;


Method Two: You can use a cookie or session

The various interactions between C # and JS

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.