asp.net method for front and back station call-practical tips

Source: Internet
Author: User
1.JavaScript accessing C # functions
2.JavaScript accessing C # variables
An existing variable that accesses JavaScript in 3.c#
4. How to access JavaScript functions in C #
JavaScript Access C # functions
method One,
1. First establish a button, in the background will call or processing content written to Button_Click;
2, in the foreground to write a JS function, content for document.getElementById ("BTN1"). Click ();
3, in the foreground or backstage call JS function, fire click event, equal access to the background C # function;
method Two,
1. function declared as public or protected
Copy Code code as follows:

public string Hello ()
{
Return ("Hello World");
}

2, in HTML with <%=fucntion ()%> can call
Copy Code code as follows:

<title>untitled page</title>
<script language= "javascript" type= "Text/javascript" >
function Say ()
{
var Strhello = "<%=hello ()%>";
alert (Strhello);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "Button1" runat= "Server" text= "button" onclientclick= "Say ();"/>
</div>
</form>
</body>

methods Three,
Copy Code code as follows:

<script language= "JavaScript" >
function __doPostBack (Eventtarget, eventargument)
{
var frontform = document. Form1; Refers to 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 ', ')" >

Accessing C # Variables in JavaScript
Method One: 1, through the page hidden domain access <input id= "xx" type= "hidden" runat= "Server" >
Method Two: 1, such as the background definition of public STRING N; foreground JS reference 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.
Existing variables that access JavaScript in C #
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
accessing JavaScript functions in C #
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.