繁体   English   中英

无法从JavaScript调用C#方法

[英]Trouble calling C# Method from javascript

我阅读了一些论坛,发现了一种从JavaScript调用C#方法的更简单方法,但是它不起作用。 我是在实时应用程序中完成此操作的,因此无法正常工作,因此我选择了一个新项目并使用以下代码:

ASPX标记

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"></asp:ScriptManager>
    <div>
    <asp:Button ID="btnMe" runat="server" OnClientClick="jsfun()" />
    </div>
    </form>
</body>
</html>

Java脚本

<script type="text/javascript">
        function jdfun() {
            PageMethods.CSFun(onSucess, onError);

        }
        function onSucess(result) {
            alert(result);
        }
        function onSucess(result) {
            alert(result);
        }

    </script>

C#

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string CSFun()
    {
        string result = "Hey Yeah";
        return result;
    }
}

没有错误,没有异常 调试器甚至不使用C#代码。 谁能帮我吗。 谢谢。

编辑

我对此并不十分了解,但是我读了一点并修复了您的代码。 这是有效的代码:

js:

<script type="text/javascript">
    function jsfun() {
        PageMethods.CSFun(onSuccess, onError);
    }
    function onSuccess(result) {
        alert(result);
    }

    function onError(result) {
        alert(result);
    }
</script>

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

</head>
<body>
    <form id="form2" runat="server">
        <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"></asp:ScriptManager>
         <div>
           <asp:Button ID="Button1" runat="server" OnClientClick="jsfun()" />
        </div>
    </form>
</body>
</html>

基本示例执行相同的操作

aspx页面

<form runat="server">
   <asp:ScriptManager ID="ScriptManager" runat="server"
       EnablePageMethods="true" />
   <fieldset id="ContactFieldset">
       <label>
           Your Name
           <input type="text" id="NameTextBox" /></label>
       <label>
           Email Address
           <input type="text" id="EmailTextBox" /></label>
       <label>
           Your Message
           <textarea id="MessageTextBox"></textarea></label>
       <button onclick="SendForm();">
           Send</button>
   </fieldset>
</form>

页面方法(.cs)

using System;
using System.Web.Services;

public partial class ContactUs : System.Web.UI.Page
{
   [WebMethod]
   public static void SendForm(string name, string email, string message)
   {
       if (string.IsNullOrEmpty(name))
       {
           throw new Exception("You must supply a name.");
       }

       if (string.IsNullOrEmpty(email))
       {
           throw new Exception("You must supply an email address.");
       }

       if (string.IsNullOrEmpty(message))
       {
           throw new Exception("Please provide a message to send.");
       }

       // If we get this far we know that they entered enough data, so
       // here is where you would send the email or whatever you wanted
       // to do :)
   }
}

javascript功能

function SendForm() {
   var name = $get("NameTextBox").value;
   var email = $get("EmailTextBox").value;
   var message = $get("MessageTextBox").value;

   PageMethods.SendForm(name, email, message,
       OnSucceeded, OnFailed);
}

function OnSucceeded() {
   // Dispaly "thank you."
   $get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
}

function OnFailed(error) {
   // Alert user to the error.
   alert(error.get_message());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM