ASP. NET frontend and backend call Method

Source: Internet
Author: User

1. Access C # functions using JavaScript
2. JavaScript accesses the C # variable
3. Access existing JavaScript variables in C #
4. How to access JavaScript Functions in C #
Access C # functions using JavaScript
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 is public or protected
Copy codeThe Code is as follows:
Public string Hello ()
{
Return ("Hello World ");
}

2. You can call <% = fucntion () %> in html.
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml%22>
<Head runat = "server">
<Title> Untitled Page </title>
<Script language = "javascript" type = "text/javascript">
Function say ()
{
Var strHello = "<% = Hello () %> ";
Alert (strHello );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" OnClientClick = "say ();"/>
</Div>
</Form>
</Body>
</Html>

Method 3,
Copy codeThe Code is as follows:
<Script language = "javascript">
Function _ doPostBack (eventTarget, eventArgument)
{
Var frontForm = document. Form1; // indicates the form of runat = server.
FrontForm. _ EVENTTARGET. value = eventTarget;
FrontForm. _ EVENTARGUMENT. value = eventArgument;
FrontForm. submit ();
}
</Script>
<Input id = "Button1" type = "button" name = "Button1" value = "button" onclick = "javascript :__ doPostBack ('button1','') ">

Access C # variables in JavaScript
Method 1: 1. Access <input id = "xx" type = "hidden" runat = "server">
Method 2: 1. For example, public string n is defined in the background; the format of the variable referenced in front-end js is '<% = n %>' or "+ <% = n %> +"
Method 3: 1. You can register a script on the page after assigning values to variables on the server.
"<Script language = 'javascript '> var temp =" + tmp + "</script>"
Tmp is the background variable, and then you can directly access temp in js to obtain the value.
Access existing JavaScript 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 backend uses request ["id"] to obtain the value;
Method 2: cookie or session can be used
Access JavaScript Functions in C #
Method 1: 1. Page. RegisterStartupScript ("ggg", "<script> SetVisible (1); </script> ");
Method 2: Use the Literal class and
Copy codeThe Code is 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;
}

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.