English 中文(简体)
从 javascript 函数调用类文件 (. c# 中的 ccs), 帮助写入 Ajax+Webservices
原标题:call class file(.cs in c#) function from javascript and helps how to write Ajax+Webservices

这里我简单的样本代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace PopUpFromCsFile
{
    public class PopUpWindow
    {
        public void PopUpFromCsFile()
        {
         string str1 = "<script> $.ajax({" +
                     "type:  GET ," +
                      "url:  Service/Class1.cs/callfromjs ," +
                        "data:  {} ," + "success: function () { getDetails(); } " +
                        "});</script>";

        page.ClientScript.RegisterStartupScript(this.GetType(), "script1", str1);

        }
        [WebMetod]
        public string  CallFromJs()
        {
            return "santosh";
        }

      public void getDetails()
       {
        string str = "<script>alert( Hai );</script>";
        System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.Handler;
        page.ClientScript.RegisterStartupScript(this.GetType(), "script2", str);
       }
    }
}

Explanation

在 PopUpFromCsFile () 中, 我为 Ajax+WebMethod 写入了代码。 Url 路径是正确的吗, 我无法从脚本中调用 Retails () 。

问题回答

t. .cs 是一个 C# 源代码文件,无法(从 anny 语言) 原样使用。您必须将其编译为 DLL 。

一旦您做了, 您就无法与 JavaScript 的 DLL 进行通信。 JavaScript 会被沙盒嵌入您的浏览器。 您需要写入( 并在每个客户端 PC 上安装) 浏览器扩展名, 以便与本地 DLL 接口 。

您需要将 C # 代码重写为 JavaScript 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签