簡體   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