Use JavaScript to call Asp.net code

Source: Internet
Author: User
1. How to access the C # variable in JavaScript?
Method 1: Use
<% = %
< Input ID = " Button3 " Type = " Button " Value = " JS calls C # VARIABLES " Onclick = " Return button3_onclick () "   />
Function Button3_onclick ()
{
Alert (
' My name: '+' <% = Name %> ');
}
CSCodeDefinition in: Protected
String Name =   " Chenlaing " ;
Method 2: Use hidden to hide the control
< Input ID = " Hidden1 " Runat = " Server " Type = " Hidden "   />
Assign a value to the hidden control in the CS code: hidden1.value
=   " Chen Liang " ;
Function Button3_onclick ()
{
Alert (document. getelementbyid (
" Hidden1 " ). Value );
}
Method 3: register the JS script in the CS code
String TMP =   " Chen quanyong " ;
Clientscript. registerclientscriptblock (this. GetType (),
Null , " <Script language = 'javascript '> var temp =' "   + TMP +   " '</SCRIPT> " );
Function Button3_onclick ()
{
Alert (temp );
}

2 . Access the C # function in javascript:
Method 1: Use
< % %>
< Script Type = "Text/JavaScript" >
VaR S =   " <% = Showbehindinfo ( " Chenliang " ) %> " ;
Document. Write (s );
</ Script >
CS background code:
Protected string showbehindinfo (string name)
{
Return "name:" + name;
}
Method 2: Use
<%   %>
< Script Type = "Text/JavaScript" >
Document. Write (
" <% Showresult (); %> " );
</ Script >
CS background code:
Protected void showresult ()
{
Response. Write ("I am Chen Quan courage ");
}
Method 3:
< Style >
. BTN
{
Display
: None ;
}
</ Style >
< ASP: button ID = "Button1" Runat = "Server" Cssclass = "BTN" Text = "Button" Onclick = "Button#click"   />
< Div Onclick = "Invokecharp ()" > Click the code to call Aspx. CS. </ Div >
The following JS Code calls the backend CS code
Function invokecharp ()
{
Document. getelementbyid ("button1"). Click ();
}
Code called through JS
Protected void button#click (Object sender, eventargs E)
{
Response. Write ("code called by JS ");
}
Method 4:
< Input Type = "Hidden" Name = "Txtfunname" >
< Input ID = "Button2" Type = "Button" Value = "Call CS functions" Onclick = "Return button2_onclick ()"   />
// Save the called information to the hidden control and submit the form.
Function button2_onclick ()
{
Document.all.txt funname. value = "show ";
Document. Forms [0]. Submit ();
}
// Backend CS code
Protected void page_load (Object sender, eventargs E)
{
String strfunname = request. Form ["txtfunname"]! = NULL? Request. Form ["txtfunname"]: "";
// Determine which function to call based on the value returned
Switch (strfunname)
{
Case "show ":
Enter (); // call this function
Break;
Default:
// Call the default Function
Break;
}
}
Method 5:
Use _ dopostback ();
< Input ID = "Button6" Type = "Button" Value = "Call the server button" Onclick = "Button6_onclick ()"   />
< ASP: button ID = "BTN" Runat = "Server" Text = "Button" Onclick = "Btn_click"   />  
< ASP: checkbox ID = "Checkbox1" Autopostback = "True" Runat = "Server"   />
Function button6_onclick ()
{
VaR res = "chenliang ";
_ Dopostback ('btn ', 'chenlinng ');
}
// The above script calls the following Event code
Protected void btn_click (Object sender, eventargs E)
{
String target = request. Params ["_ eventtarget"];
String ARGs = request. Params ["_ eventargument"];
Response. Write (target +"
< BR > "+ ARGs );
}
_ Dopostback explanation:
Is a pure and very simple JavaScript function.
For example, if the ASPX page contains an ASP: linkbutton or a server control with the autopostback attribute and its value is true
ASP. NET will automatically generate the following script for the page
Actually, the _ dopostback function is called.
That is, the control that triggers sending back (the first parameter) and related parameters (the second parameter) are paid to the hidden control of the two pages.
Then, submit the form;
< Input Type = "Hidden" Name = "_ Eventtarget" ID = "_ Eventtarget" Value = ""   />
< Input Type = "Hidden" Name = "_ Eventargument" ID = "_ Eventargument" Value = ""   />
Function _ dopostback (eventtarget, eventargument)
{
If (! Theform. onsubmit | (theform. onsubmit ()! = False ))
{
Theform. _ eventtarget. value = eventtarget;
Theform. _ eventargument. value = eventargument;
Theform. Submit ();
}
}
In the background Event code, you can
Use request. Form ["_ eventtarget"] to obtain the event source (Control ID) for triggering the page PostBack)
However, the PostBack triggered by button and imagebutton cannot obtain their IDs in this way.

FromHttp://hi.baidu.com/chen839357/blog/item/42bb9594c12d4047d0135e60.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.