开发者

Embedding jQuery into your ASP.Net Server Custom Control

I am undergraduate student. I had some queries related to embedding jQuery inside your ASP.NET Server side Custom Control.

private string GetEmbeddedTextFile(string sTextFile) { // generic function for retrieving the contents // of an embedded text file resource as a string

        // 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
      开发者_如何学Go  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();
    }

but the problem I am facing is that all my JQuery code which I have written in separate .JS file and have tried to embed in my Custom control , is streamed as output on the screen . Also , my Control is not behaving correctly as it should have to , jQuery functionality is not working behind due to this reason of not getting embedded properly :-( Please help me out! Thank you!


It sounds like you are missing the <script> tags surrounding the javascript. Try this overload of RegisterClientScriptBlock that takes a fourth boolean parameter that will add the script tags if it is true.

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


"all my JQuery code which I have written in separate .JS file and have tried to embed in my >Custom control , is streamed as output on the screen" .

Sounds like you need to add a line that tells the code how to download the embedded javascript. Do it with an Assembly annotation above the start of a class in your custom control project. This is the format:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜