JS call background, background call foreground and other methods to summarize _javascript skills

Source: Internet
Author: User

JS call backstage, background call foreground and other methods summary

1. 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 button1_click;

Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
This. TextBox1.Text = "Voodooer";
}

2, in the foreground can be called:
Copy Code code as follows:

<input type= "button" value= "Access to C # 's method" Onclick= ' document.getElementById ("Button1"). Click (); '/>

Method Two: 1, function declared as public or protected
Copy Code code as follows:

public string SS ()
{
Return ("Voodooer");
}

2, the front call method
Copy Code code as follows:

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

Method Three: 1, <script language= "JavaScript" >
Copy Code code as follows:

<!--
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 type= "button" value= "Buttons" >

Method Four:
Copy Code code as follows:

<script language= "JavaScript" >
function Submitkeyclick ()
{
if (Event.keycode = 13)
{
Event.cancelbubble = true;
Event.returnvalue = false;
Document.all.funname.value= "The function name you want to call";
Document.form[0].submit ();
}
}
</script>
<input type= "Text" >
<input type= "hidden" >〈! --To store the function you want to call--〉

In the. cs there are:
Copy Code code as follows:

Public Page_onload ()
{
if (! Page.ispost ())
{
String strfunname=request.form["Funname"]!=null? request.form["Funname"]: "";
Decide which function to call based on the returned value
Switch (strfunname)
{
Case "Enter ()":
Enter (); Call this function
Break
Case "Other":
Calling other functions
Break
Default
Calling the default function
Break
}
}
}
public void Enter ()
{
...... Like calculating a value
}

2. How do I access C # variables in JavaScript?

The answer is as follows:

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

Method Two: 1, such as the background definition of public STRING N; foreground JS reference to the variable in the format of ' <%=n%> ' or ' + <%=n%>+ '

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

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

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

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

The answer is as follows:

Method One: 1, the foreground uses the static text control to hide the domain, writes the JS variable value to it;

2, the backstage uses request["the ID" to obtain the value;

Method Two: Can use Cookie or session

4. How do I access JavaScript functions in C #?

The answer is as follows:

Execute JavaScript functions in C # code:

Method One: 1, page.registerstartupscript ("GGG", "<script>setvisible (1); </script>");

Method Two: Use the literal class, and then
Copy Code code as follows:

private void Button2_Click (object sender, System.EventArgs e)
{
String str;
str= "<script language= ' JavaScript ' >";
str+= "Selectrange ()";
str+= "</script>";
Literal1.visible=true;
LITERAL1.TEXT=STR;
}

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.