如何调用后面的服务器方法code从客户端的JavaScript函数?函数、客户端、后面、服务器

由网友(够狠才是男人)分享简介:我有一个JavaScript函数在ASPX页面的HTML按钮单击事件。而在其身后页code服务器的方法。现在我想与点击用户的HTML按钮,只有当一些参数调用的javascript函数的服务器的方法。请不要改变这一局面,同时也回答不使用任何asp.net contols在aspx页面。因为只有HTML控件是允许的。任何人...

我有一个JavaScript函数在ASPX页面的HTML按钮单击事件。而在其身后页code服务器的方法。现在我想与点击用户的HTML按钮,只有当一些参数调用的javascript函数的服务器的方法。请不要改变这一局面,同时也回答不使用任何asp.net contols在aspx页面。因为只有HTML控件是允许的。任何人都可以帮助我在此?提前致谢。翘首等待答案。

下面是code,

code的标记:

 < SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
    传播btnAccept_onclick(){
        变种的名字;
        NAME =的document.getElementById('改为txtName)的价值。

        //调用服务器端方法SETNAME()通过传递此参数'名'
< / SCRIPT>

<输入类型=按钮ID =btnAccept值=接受的onclick =返回btnAccept_onclick()/>
 

code-背后:

 公共无效SETNAME(字符串名称)
{
    // code的某些功能
}
 
利用VS Code进行远程开发,就问你香不香

解决方案

如..

是的,你可以让Web方法

  [WebMethod的]
公共静态字符串SETNAME(字符串名称)
{
    返回你的字符串
}
 

,然后在javascript调用一样..

PageMethods.SetName(parameterValueIfAny,onSuccessMethod,onFailMethod);

这也需要

< ASP:ScriptManager的ID =ScriptMgr=服务器的EnablePageMethods =真正的>                 < / ASP:ScriptManager的>

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    
}

解决方案

yes, you can make web method like..

[WebMethod]
public static String SetName(string name)
{
    return "Your String"
}

and then call in javascript like..

PageMethods.SetName(parameterValueIfAny, onSuccessMethod,onFailMethod);

this also required

<asp:ScriptManager ID="ScriptMgr" runat="server" EnablePageMethods="true"> </asp:ScriptManager>

阅读全文

相关推荐

最新文章