简体   繁体   English

如何在JavaScript中使用C#代码

[英]How to use c# code inside javascript

I am new to javascript. 我是javascript新手。 i would like to write asp.net code using script. 我想用脚本编写asp.net代码。 using the code shown below doesn't give any output.or should i use ajax to access asp.net file 使用下面显示的代码没有给出任何输出。或者我应该使用ajax来访问asp.net文件

here my sample code : a.html 这是我的示例代码:a.html

<script runat="server" language="c#" type="text/c#">

 protected void Page_Load(Object s, EventArgs e){

    Response.Write("Welcome to our web site.");

}

</script>

<body>

</body>

If you want to access C# code,you have to write Webmethod... 如果要访问C#代码,则必须编写Webmethod ...

You have to place a scriptmanager.. 您必须放置一个脚本管理器。

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

 [WebMethod]
    public static string Abc()
    {
    }

You can access the C# method in script as follows.. 您可以按以下方式在脚本中访问C#方法。

function Test()
{
PageMethods.Abc(onSucess, onError);

        function onSucess(result) {
          alert("Success");
        }
        function onError(result) {
            alert('Cannot process your request at the moment, please try later....');
        }
        return false;
}

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

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