Saturday 15 February 2014

c# - How to call code behind server method from a client side JavaScript function? -


I have a JavaScript function for the HTML button click event in the ASPX page. And a server method in my code behind the page. Now I want to call the Javascript function server method from some parameters, when the user clicks on the HTML button.

Please do not make changes in this scenario and do not use any asp.net contols in the aspx page during reply. Because only HTML control is allowed, can anyone help me on this? Thanks in advance.

In the code markup:

  & lt; Script language = "javascript" type = "text / javascript" & gt; Function btnAccept_onclick () {var name; Name = document.getElementById ('txtName'). Values; // This parameter is called 'name' and lt; / Script & gt; Please enter server-side method SetName () by passing. & Lt; Input type = "button" id = "btnAccept" value = "accept" onclick = "return btnAccept_onclick ()" />   

Code-Back:

  Public Zero SetName (string name) {// code for some functionality}   < / Div> 

Yes, you can create a web method ..

  [WebMethod] public Constant String Setname (name of string) {Return your "string"}   

and then call it in javascript,

  PageMethods.SetName (parameterValueIfAny, OnSuccessMethod, onFailMethod);   

Also required:

  & lt; Asp: ScriptManager id = "ScriptMgr" runat = "server" EnablePageMethods = "true" & gt; & Lt; / ASP: ScriptManager & gt;    

No comments:

Post a Comment