views:

269

answers:

1

Hello ! 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
        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!

+1  A: 

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);
jrummell