I am having an JavaScript function for a HTML button click event in ASPX page. And a server Method in its code behind page. Now i want to call the server method from the JavaScript function with some parameters only when the HTML button is clicked by the user.
Please don't change this scenario and also don't use any asp.net contols in the aspx page while replying. Because only HTML controls are allowed. Can anyone help me on this ?. Thanks in advance. Eagerly waiting for answers.
Here is the code,
Code in markup:
<script language="javascript" type="text/javascript"> function btnAccept_onclick() { var name; name = document.getElementById('txtName').value; // Call Server side method SetName() by passing this parameter 'name' </script> <input type="button" id="btnAccept" value="Accept" onclick="return btnAccept_onclick()" />
Code-behind:
public void SetName(string name) { // Code for some functionality }
You cannot just call JavaScript from server side.
You cannot. Codebehind is running on the server while JavaScript is running on the client.
PageMethods is a JavaScript class that is used to call code-behind functions via AJAX calls in the background. For example: You can use PageMethods to call a C# function on server-side. If PageMethods is not working in your application, there might be an implementation or post-back issue.
Yes, you can make a web method like..
[WebMethod] public static String SetName(string name) { return "Your String" }
And then call it in JavaScript like,
PageMethods.SetName(parameterValueIfAny, onSuccessMethod,onFailMethod);
This is also required :
<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true"></asp:ScriptManager>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With