JS call background method JS call background Method

Source: Internet
Author: User
JS call background Method
Http://netcode.cnblogs.com/archive/2011/05/19/js-csharp.html
I. ajaxpro usage 1. add reference in the project, browse to find the ajaxpro.2.dll file 2. in the web. system. write the following code in the Web </configuration> <system. web>
2. Direct call: In javascript: <% = background method %> function says () {alert ("<% = say () %>");} function del () {alert ("<% = deletebyid (8) %>"); // deletebyid (8) Background method name} 3. Callback using icallbackeventhandler // system must be declared. web. UI. icallbackeventhandler interface public partial class _ default: system. web. UI. page, system. web. UI. icallbackeventhandler {// defines the private string result returned by a callback; // defines two variables to receive the private string num1, private string num2, and protected void page_load (Object sender, eventargs e) {}/// <summary> // This method is the method for callback execution. The callback content is processed according to parameters in this method, this method does not return a value /// </Summary> /// <Param name = "eventargument"> this parameter is passed from the client </param> Public void raisecallbackevent (string eventargument) {// eventargumeng is the parameter passed by JavaScript from the client. In this example, the three parameters are separated by "/" and each parameter is retrieved and saved to the array string [] pagparams = eventargument. split ('/'); num1 = pagparams [1]; num2 = pagparams [2]; // based on the first parameter (selected operator ), call different execution functions switch (pagparams [0]) {Case "0": Result = add (); break; Case "1": Result = sub (); break; case "2": Result = multi (); break; Case "3": Result = Division (); break ;}} /// <summary> /// this method returns the callback result to the client // </Summary> /// <returns> </returns> Public String getcallbackresult () {return result;} // The following four functions use the raisecallbackevent method. The called callback function private string add () {double addresult = double. parse (num1) + double. parse (num2); Return addresult. tostring ();} private string sub () {double addresult = double. parse (num1)-double. parse (num2); Return addresult. tostring ();} private string multi () {double addresult = double. parse (num1) * double. parse (num2); Return addresult. tostring ();} private string Division () {double addresult = double. parse (num1)/double. parse (num2); Return addresult. tostring ();}}
In my implementation, there is a textbox and a common button at the front end. click the button to insert the value in the textbox into the database.
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Input id = "button1" type = "button" value = "click my" onclick = "test ();"/>
Function Test ()
{
VaR names = Document. getelementbyid ("textbox1"). value;
VaR result = _ default. insert (names). value;
Alert (result );
}
Background # Region
[Ajaxpro. ajaxmethod]

Public static string insert (string name)
{

String flag = "submission failed ";
Sqlconnection conn = dbopen. dbaseconfigconnect ();
Sqlcommand command = new sqlcommand ();
Command. Connection = conn;
String STR = "Update wblxdjb set WXR = '" + name + "'where bxbh = 'dia201 '";
Command. commandtext = STR;
If (command. executenonquery () = 1)
Flag = "submitted ";
Return flag;

}
# Endregion
Protected void page_load (Object sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
}
Remember to reference ajaxpro.2.dll
Web. config
    <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
       
Http://netcode.cnblogs.com/archive/2011/05/19/js-csharp.html
I. ajaxpro usage 1. add reference in the project, browse to find the ajaxpro.2.dll file 2. in the web. system. write the following code in the Web </configuration> <system. web>
2. Direct call: In javascript: <% = background method %> function says () {alert ("<% = say () %>");} function del () {alert ("<% = deletebyid (8) %>"); // deletebyid (8) Background method name} 3. Callback using icallbackeventhandler // system must be declared. web. UI. icallbackeventhandler interface public partial class _ default: system. web. UI. page, system. web. UI. icallbackeventhandler {// defines the private string result returned by a callback; // defines two variables to receive the private string num1, private string num2, and protected void page_load (Object sender, eventargs e) {}/// <summary> // This method is the method for callback execution. The callback content is processed according to parameters in this method, this method does not return a value /// </Summary> /// <Param name = "eventargument"> this parameter is passed from the client </param> Public void raisecallbackevent (string eventargument) {// eventargumeng is the parameter passed by JavaScript from the client. In this example, the three parameters are separated by "/" and each parameter is retrieved and saved to the array string [] pagparams = eventargument. split ('/'); num1 = pagparams [1]; num2 = pagparams [2]; // based on the first parameter (selected operator ), call different execution functions switch (pagparams [0]) {Case "0": Result = add (); break; Case "1": Result = sub (); break; case "2": Result = multi (); break; Case "3": Result = Division (); break ;}} /// <summary> /// this method returns the callback result to the client // </Summary> /// <returns> </returns> Public String getcallbackresult () {return result;} // The following four functions use the raisecallbackevent method. The called callback function private string add () {double addresult = double. parse (num1) + double. parse (num2); Return addresult. tostring ();} private string sub () {double addresult = double. parse (num1)-double. parse (num2); Return addresult. tostring ();} private string multi () {double addresult = double. parse (num1) * double. parse (num2); Return addresult. tostring ();} private string Division () {double addresult = double. parse (num1)/double. parse (num2); Return addresult. tostring ();}}
In my implementation, there is a textbox and a common button at the front end. click the button to insert the value in the textbox into the database.
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Input id = "button1" type = "button" value = "click my" onclick = "test ();"/>
Function Test ()
{
VaR names = Document. getelementbyid ("textbox1"). value;
VaR result = _ default. insert (names). value;
Alert (result );
}
Background # Region
[Ajaxpro. ajaxmethod]

Public static string insert (string name)
{

String flag = "submission failed ";
Sqlconnection conn = dbopen. dbaseconfigconnect ();
Sqlcommand command = new sqlcommand ();
Command. Connection = conn;
String STR = "Update wblxdjb set WXR = '" + name + "'where bxbh = 'dia201 '";
Command. commandtext = STR;
If (command. executenonquery () = 1)
Flag = "submitted ";
Return flag;

}
# Endregion
Protected void page_load (Object sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
}
Remember to reference ajaxpro.2.dll
Web. config
    <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
       

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.