Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed JQuery into custom server control

I am writing a server control that is a search box with suggestions (when the user starts typing, matching words will appear for them to select). I am using jquery and C#.

The control works fine when I include the jquery Lib on the page that I am testing on. However, it will not work when I attempt to embed the lib in the DLL.

I have tried completely embedding it and registering the script:

ClientScriptManager manager = this.Page.ClientScript;
manager.RegisterClientScriptResource(typeof(Search), jqueryResource); //jqueryResource= SearchControl.javascript.jquery.js 

and I am also tried just adding a tag in the header of the page that includes the jquery:

string javascriptUrl = manager.GetWebResourceUrl(typeof(Search), jqueryResource); 
LiteralControl jquery = new LiteralControl(string.Format(javascriptInclude, jqueryUrl));
Page.Header.Controls.Add(jquery);

Both ways cause javascript errors (Object Expected) when trying to get information from controls

var params = $("#searchBox").val(); //called on keyup when typing in textbox

Has someone done this before? Is it possible? Can someone shed some light on this?

PS: Assemblies are already registered in the AssemblyInfo.cs file.

like image 937
The Sheek Geek Avatar asked Dec 08 '09 20:12

The Sheek Geek


People also ask

How do I add jQuery to my project?

There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN). Note: A Content Delivery Network (CDN) is a system of multiple servers that deliver web content to a user based on geographical location.

Can we use jQuery in ASP NET?

JQuery is a JavaScript library. It is helpful and make easy to handle HTML DOM (Document Object Model), Events and Animation and Ajax functionalities. JQuery reduce code compared to JavaScript. Mostly we use JQuery or JavaScript for client side activities and make Ajax call to ASP.NET Web form/mvc, Web service and WCF.

Can we include jQuery in JavaScript?

jQuery code is implemented as part of JavaScript scripts. To add jQuery and JavaScript to your web pages, first add a <script> tag that loads the jQuery library, and then add your own <script> tags with your custom code.

What is jQuery explain different ways of adding jQuery to a Web page?

Like any other JavaScript library, jQuery is added to a webpage by placing the script tag inside the head tag with the src of the script tag pointing towards the jQuery library, as shown below. There are two main ways to point the src towards the jQuery library: Downloading the jQuery library.


1 Answers

EDIT: Just re-read your question and realised I've just repeated what you've done. However this is my implementation which works as expected:

You need to use RegisterClientScriptResource. Just call the following snippet (taken from my ScriptRegisterHelper static class) in the PreRender event of the control:

/// <summary>
/// Registers the jQuery client script to the page.
/// </summary>
/// <param name="cs">The ClientScriptManager to assign the script to.</param>
internal static void RegisterJQuery(ClientScriptManager cs)
{
    cs.RegisterClientScriptResource(typeof(ScriptRegisterHelper), 
        "MyProject.Resources.jquery.js");
}

And for completion, here's the implentation:

protected override void OnPreRender(EventArgs e)
{
    if (!this.DesignMode)
    {

       // Register the JavaScript libraries
       ClientScriptManager cs = this.Page.ClientScript;
       ScriptRegisterHelper.RegisterJQuery(cs);

    }
}

And the AssemblyInfo file:

[assembly: System.Web.UI.WebResource("MyProject.Resources.jquery.js", "text/javascript")]
like image 185
djdd87 Avatar answered Oct 18 '22 00:10

djdd87