1

こんにちは、この質問があります。私は asp コントローラーから JavaScript 関数を呼び出す方法を見つけようとしていました。コードは次のとおりです。

    <script type="text/javascript">
      function hello() { 
        alert("hello world")
    }
</script>
<  /head>
 <body>
<form id="form1" runat="server">
<div>
    <asp:Button ID="buttonme" runat="server" OnClientClick="javascript:hello()" 
 Text="click" />

今、私はこのコードビハインド関数を持っているとしましょう

    Public Sub msg()
      MsgBox("hello world")
    End Sub 

そして、JavaScript関数から呼び出したいので、コントローラー----呼び出し---> javascript ---call--->コードビハインドのようになります

これを行う方法はありますか

4

2 に答える 2

4

Web アプリケーションのどこかにあるクラスでメソッドを定義します。

[System.Web.Services.WebMethod]
public static string Msg()
{
     return "Hello world";
}

aspx ページに EnablePageMethods=true でスクリプト マネージャーを追加します。

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

メソッドを JavaScript で呼び出す

<script language="javascript" type="text/javascript">
    PageMethods.Msg(onSuccess);
    function onSuccess(response)
    {
        alert(response);
    }
</script>
于 2012-06-14T19:03:10.467 に答える
0

なんらかの形式で AJAX を使用し、HTTP 経由で呼び出す ASP.NET メソッドを公開する必要があります。

于 2012-06-14T18:57:14.460 に答える