【问题标题】:Embedding jQuery into your ASP.Net Server Custom Control将 jQuery 嵌入到您的 ASP.Net 服务器自定义控件中
【发布时间】:2011-01-26 01:40:05
【问题描述】:

我是本科生。我有一些与在 ASP.NET 服务器端自定义控件中嵌入 jQuery 相关的查询。

私有字符串 GetEmbeddedTextFile(string sTextFile) { // 检索内容的通用函数 // 嵌入文本文件资源的字符串

        // we'll get the executing assembly, and derive
        // the namespace using the first type in the assembly
        Assembly a = Assembly.GetExecutingAssembly();
        String sNamespace = a.GetTypes()[0].Namespace;

        // with the assembly and namespace, we'll get the
        // embedded resource as a stream
        Stream s = a.GetManifestResourceStream(
                    string.Format("{0}.{1}",a.GetName().Name, sTextFile)
                );

        // read the contents of the stream into a string
        StreamReader sr = new StreamReader(s);
        String sContents = sr.ReadToEnd();

        sr.Close();
        s.Close();

        return sContents;
    }
    private void RegisterJavascriptFromResource()
    {
        // load the embedded text file "javascript.txt"
        // and register its contents as client-side script
        string sScript = GetEmbeddedTextFile("JScript.txt");
        this.Page.RegisterClientScriptBlock("PleaseWaitButtonScript", sScript);
    }
    private void RegisterJQueryFromResource()
    {
        // load the embedded text file "javascript.txt"
        // and register its contents as client-side script
        string sScript = GetEmbeddedTextFile("jquery-1.4.1.min.txt");
        this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jQuery", sScript);
       // this.Page.RegisterClientScriptBlock("JQueryResourceFile", sScript);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        // the client-side javascript code is kept
        // in an embedded resource; load the script
        // and register it with the page.
        RegisterJQueryFromResource();
        RegisterJavascriptFromResource();
    }

但我面临的问题是,我在单独的 .JS 文件中编写并尝试嵌入自定义控件的所有 JQuery 代码都在屏幕上作为输出流式传输。另外,我的控件没有正常运行,由于没有正确嵌入,jQuery 功能无法正常工作:-( 请帮帮我! 谢谢!

【问题讨论】:

    标签: asp.net jquery streaming custom-controls embed


    【解决方案1】:

    听起来您缺少围绕 javascript 的 <script> 标记。试试这个RegisterClientScriptBlock 的重载,它接受第四个布尔参数,如果它是真的,它将添加脚本标签。

    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "jQuery", sScript, true);
    

    【讨论】:

      【解决方案2】:

      “我在单独的 .JS 文件中编写并尝试嵌入到我的 >Custom 控件中的所有 JQuery 代码都作为输出流式传输到屏幕上”。

      听起来您需要添加一行来告诉代码如何下载嵌入的 javascript。使用自定义控件项目中类的开头上方的 Assembly 注释来执行此操作。这是格式:

      <Assembly: System.Web.UI.WebResource("Namespace.ScriptName.js", "application/x-javascript")> 
      

      【讨论】:

        猜你喜欢
        • 2010-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-05
        • 1970-01-01
        相关资源
        最近更新 更多