JS calls. NET background events, and background calls to the foreground and other methods as well as JS call server control methods

Source: Internet
Author: User

http://blog.csdn.net/deepwishly/article/details/6670942 AjaxPro.dll Basic Tutorial (foreground call background method, background call foreground method)

1. Perform functions in C # code in JavaScript functions:

Method One: Indirectly trigger the background code

1, first set up a service-side control button named BTN1, double-click into the background will be called or processed content written to Btn1_click;

2, write a JS function in the foreground, the content is Document.getelementbyidx ("Btn1"). Click ();

3, in the foreground call JS function, fire click event, equal access to the background C # function;

The code is as follows:

<input id= "hBt" type= "button" value= "click ME2" onclick= "fun2 ()"/>

<asp:button id= "aBt" runat= "Server" text= "Server BT" onclick= "Abt_click"/>

<asp:label id= "Label1" runat= "Server" text= "Label" ></asp:Label>
Assign a value to lable when you click on the Server Side button

protected void Abt_click (object sender, EventArgs e)

{Label1.Text = "true false ~ ~ ~"; }

The JS code is as follows:

function fun2 () {

document.getElementById (' ABt '). Click ();



Method Two: 1, function declaration is public

Background code (change public to protected also can)

public string SS ()


Return ("a");


2, in the HTML with <%=fucntion ()%> can call

Foreground script

<script language=javascript>

var a = "<%=ss ()%>";

alert (a);


in the form of a parameter

All are HTML control input name Click button to get the information you entered!

The code is as follows:
Your name Is:<input id= "name" type= "text"/>

<input id= "CLICKBT" type= "button" value= "GET NAME" onclick= "Fun ()"/>
<input id= "Nametxt" type= "text"/>

JS as follows:
language= "JavaScript" type= "Text/javascript" >
function Fun ()
var name = document.getElementById (' name '). Value;
var nameStr =
' <%=getname ("' +name+ '")%> '; The page is loaded at the first time, due to the order of execution of the page, the time to run to this point back to the "+ name +" value to the background, but does not really perform the background method

document.getElementById (' Nametxt '). Value = NAMESTR;


The background code is as follows:
Public string GetName (String namepar)
Return Namepar + "is your name";

Method Three: 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 ();




<input type= "button" value= "Buttons" >

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 ();




<input type= "Text" >

<input type= "hidden" >〈! --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"]: "";

Determines which function to call based on the value returned

Switch (strfunname)


Case "Enter ()":

Enter (); Call this function


Case "Other":

Calling other functions



Calling the default function





public void Enter ()


...... such as calculating a value


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

The answers are as follows:

Method One: 1, Access <input type= "hidden" runat= "Server" by hiding the domain on the page >

Method Two: 1, such as the background defines the public STRING N; The format for referencing the variable in the foreground JS is ' <%=n%> ' or ' + <%=n%>+ '

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

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

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

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

The answers are as follows:

Method One: 1. The foreground uses the static text control to hide the field and writes the value of the JS variable to it;

2, backstage with request["id" to get the value;

Method Two: You can use a cookie or session

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

The answers are 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

private void Button2_Click (object sender, System.EventArgs e)


String str;

str= "<script language= ' JavaScript >";

str+= "Selectrange ()";

str+= "</script>";




Originally from: http://hi.baidu.com/xiaowei0705/blog/item/4d56163f5e4bf616bba16725.html

How clients get the ASP. NET Server Control ID classification: C # The ID of the server control in ASP. NET will change a certain amount on the client. How to use document.getElementById () to get the values in a control becomes a challenge. For example: In a login page, put an Account entry box, a password input box, a submit button

<asp:textbox id= "Textbox_uid" runat= "Server" maxlength= "></asp:TextBox>"

<asp:textbox id= "TEXTBOX_PWD1" runat= "Server" maxlength= "textmode=" Password "></asp:TextBox>

<asp:button id= "Button_login" runat= "server" text= "login" onclick= "Button_login_click" onclientclick= "return Checkreg () "/>

How do I get the ID of a server control exactly? See below: Using the <%= control id.clientid%>

In order to reduce the frequent submission of inputs. I write a JavaScript method on the client, which simply determines whether the input value meets the requirements. If the account and password input is not empty, I will submit to the background to judge.
The control TextBox's ID is textbox_uid and textbox_pwd1 but the client turns into Ctl00_textbox_user and ctl00_textbox_pwd.

<script language= "javascript" type= "Text/javascript" >

Check if the account number and password are empty

function Checkreg ()


var uid = document.getElementById ("<%=textbox_uid.clientid%>"). Value;

var pwd1 = document.getElementById ("<%=textbox_pwd1.clientid%>"). Value;

if (uid = = "" "| | PWD1 = = "")


Alert ("The account and password cannot be empty!") ");

return false;


return true;



Transferred from: http://www.cnblogs.com/qinpengming/archive/2012/05/23/2514373.html

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.