Asp. NET in the foreground JavaScript and background code calls

Source: Internet
Author: User

Asp. NET in the foreground JavaScript and background code calls
1 is JavaScript accessing C # functions?
2. If Ann is accessing C # variables in JavaScript?
3. If Ann is accessing JavaScript's already variable in C #?
4. If Ann is accessing JavaScript functions in C #?
Title 1 answers to the following:
The functions in C # code are implemented in JavaScript functions:
Method 1:1, the initial set up a button, is the background will invoke the content of the call may be written in Button_Click;
Second, is writing a JS function at the foreground, the content is document.getElementById ("Btn1"). Click ();
Third, the foreground may invoke the JS function background, fire Click event, that is, access to the background C # function;
Method 2:1, function declaration is public
The following is what is invoked:
Background code (change public to protected)
public string SS ()
{
Return ("a");
}
Second, in the HTML is used to call
Front desk Script
<script language=javascript>
var a = "<%=ss ()%>";
alert (a);
</script>
Method 3: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= "button"
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 onkeypress= "Submitkeyclick ()" id= "AAA" type= "text" >
<input type= "hidden" name= "Funname" >〈! --Used to store the function you want to call--〉
Is. CS is available in:
The following is what is invoked:
Public Page_onload ()
{
if (! Page.ispost ())
{
String strfunname=request.form["Funname"]!=null? request.form["Funname"]: "";
Determine which function to invoke by following 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 ()
{
...... A metaphor for a value
}
Heading 2. are JavaScript accessing C # variables?
Answer the following:
Method 1:1, through hidden domain access on the page
<input id= "xx" type= "hidden" runat= "Server" >
Method 2:1, such as the background concept of public STRING N; The format for invoking this variable in the foreground JS is "maybe" + + "
Method 3:1, approximately you can register a script on the page after assigning a value to the server side variable
"<script language= ' JavaScript ' >var temp=" + tmp + "</script>"
TMP is the background variable, and then JS can indirectly access the temp to get the value.
Heading 3. If Ann is accessing JavaScript's already variable in C #?
Answer the following:
Method 1:1, the foreground uses the static text control to hide the field, writes the JS variable value to this, second, the background uses the request["the ID" to obtain the value;
Method Two: You can use a cookie or a session
Heading 4. If Ann is accessing JavaScript functions in C #?
Answer the following:
Implement JavaScript functions in C # code:
Method 1:1,
Page.registerstartupscript ("GGG", "" ");
Method Two: Use the literal class, and then

private void Button2_Click (object sender, System.EventArgs e)
{
String str;
Str= "";
Literal1.visible=true;
LITERAL1.TEXT=STR;
}

Asp. NET in the foreground JavaScript and background code calls

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.