Asp.net frontend and backend function variable mutual access

Source: Internet
Author: User

1. How to access the C # function in JavaScript?

2. How to access the C # variable in JavaScript?

3. How to access existing JavaScript variables in C?

4. How to access JavaScript Functions in C?

The answer to question 1 is as follows:

Execute functions in C # code in javaScript Functions:

Method 1:

1. Create a button to write the called or processed content to button_click in the background;

2. Write a js function on the front-end with the content document. getElementById ("btn1"). click ();

3. Call js functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the c # function in the background;

Method 2:

1. function declaration as public background code (change public to protected) public string ss () {return ("");}

2. You can use <% = fucntion () %> in html to call the foreground script <script language = javascript> var a = "<% = ss () %> "; alert (a); </script>

Method 3:

1. <script language = "javascript">

<! -- Function _ doPostBack (eventTarget, eventArgument) {var theForm = document. form1; // indicates the form theForm of runat = server. _ EVENTTARGET. value = eventTarget; theFrom. _ EVENTARGUMENT. value = eventArgument; theForm. submit () ;}->

</Script>

<Input id = "Button1" type = "button" name = "Button1" value = "button" onclick = "javascript :__ doPostBack ('button1','') ">

When An ASPX page contains an asp: LinkButton or a server control with the AutoPostBack attribute and its value is true, ASP. NET automatically generates the following script for the page:

<Input type = "hidden" name = "_ EVENTTARGET" id = "_ EVENTTARGET" value = ""/>

<Input type = "hidden" name = "_ EVENTARGUMENT" id = "_ EVENTARGUMENT" value = ""/>

The two parameters are saved by the hidden form fields _ EVENTTARGET and _ EVENTARGUMENT.

The two hidden forms can be used to find the control ID and parameters causing page delivery:

String target = Request. Params ["_ EVENTTARGET"];

String args = Request. Params ["_ EVENTARGUMENT"];

Asp: Button and asp: ImageButton do not use _ doPostBack to send back pages. Therefore, the above Code is invalid when the two controls are used to send back pages.

Method 4:

<Script language = "javascript">

Function SubmitKeyClick ()

{If (event. keyCode = 13)

{Event. cancelBubble = true; event. returnValue = false; document. all. funName. value = "Name of the function you want to call"; 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-> -〉

In. CS, there are:

Public Page_OnLoad ()

{If (! Page. IsPost ())

{String strFunName = Request. Form ["FunName"]! = Null? Request. Form ["FunName"]: "";

// Determine which function to call based on the value returned

Switch (strFunName) {case "enter ()": enter ();

// Call the break function;

Case "other": // call other functions

Break;

Default: // call the default function break ;}}}

Public void enter ()

{//...... For example, calculating a value}

Question 2. How to access the C # variable in JavaScript?

The answer is as follows:

Method 1:

1. Access <input id = "xx" type = "hidden" runat = "server">

Method 2:

1. If the backend defines public string n, the format of the variable referenced in front-end js is '<% = n %>' or "+ <% = n %> +"

Method 3:

1. You can register a script "<script language = 'javascript '> var temp =" + tmp + "</script>" tmp is background variable,

Then, you can directly access temp in js to obtain the value.

3. How to access existing JavaScript variables in C? The answer is as follows:

Method 1:

1. the foreground uses static text controls to hide fields and write js variable values into them;

2. the backend uses request ["id"] to obtain the value;

Method 2: cookie or session can be used

4. How to access JavaScript Functions in C? The answer is as follows:

C # execute javaScript Functions in the Code:

Method 1: 1. Page. RegisterStartupScript ("ggg", "<script> SetVisible (1); </script> ");

Method 2: Use the Literal class and

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 ;}

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: [email protected] 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.