2010-03-10 46 views
1

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

私人字符串GetEmbeddedTextFile(字符串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的功能没有正常工作,原因是没有正确嵌入:-( 请帮我一下! 谢谢!

回答

1

听起来像是你缺少<script>标签周围的JavaScript。试试这个超负荷的RegisterClientScriptBlock这需要将添加脚本标签,如果它是真实的第四布尔参数。

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

“这是我写的都是我的jQuery代码单独的.JS文件,并试图嵌入我>自定义控件,是流编辑为屏幕上的输出“。

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

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