1

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!

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

"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")> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.