views:

180

answers:

6

I have a user control with a "More Info" link. When clicking that link, I want a div to appear which gives the user more information.

Where do I put the javascript? It doesn't appear to work when placing at the top of my user control. How do you handle javascript and jquery when you are using a user control? I have jquery included in my master page. My regular asp.net pages have javascript and those work.

EDIT: It appears that the way to do client side coding in a user control is to do output a script via server side, similar to how the asp.net server controls work. What is disappointing is that jquery is so awesome but all the examples are strictly embedded on the html/aspx page. Is there a resource out there for jquery with asp.net that I haven't found yet?

A: 

You can dynamically add it bia backend logic, but as long as your host page that you are planting the user control into has a reference to said js file, you should be good.

jlech
+1  A: 

There are many ways to do this. You can just include javascript anywhere and setup an event for that click.

If the javascript is a part of that user control only, and you only want it to be included with the user control, you can put it in a web resource and access it from there.

http://bchavez.bitarmory.com/archive/2008/07/28/understanding-scriptresource-and-webresource-in-asp.net.aspx

You should be able to put tags in your user control also.

Josh Close
do you have a link to where I can figure out how to access it in a web resource? Thanks
Scott
I added a link to an example of using web resources. This is what most 3rd party purchased custom controls use, so they only have to ship a DLL and not a bunch of scripts, html, css, etc files. http://bchavez.bitarmory.com/archive/2008/07/28/understanding-scriptresource-and-webresource-in-asp.net.aspx
Josh Close
Thanks Josh, much appreciated!
Scott
+2  A: 

Use ClientScriptManager.RegisterStartUpScript to set your scripts at the end of the page at server side. Or use ready event of the JQuery ensure the dom is loaded, and then bind your client side events to your link.

RegisterStartupScript, registers your scripts at the end of the page, so your script loaded after all the elements of the page loaded.

One other option is to use ready event of the JQuery, it helps you to wait all elements of DOM loaded. You can implement your ready function at inline or server side.

The key point here is to wait for the DOM elements load.

Canavar
The link for Page.RegisterStarUpScript notes that this is Obsolete and you should use ClientScript.RegisterStartupScript instead.
chollida
@chollida : Thanks, you're right, I wanted to answer quickly :)
Canavar
So the key for this when using a user control is to output the script on server side?
Scott
@Scott : Please see my updated answer.
Canavar
A: 

if you are using a usercontrol with a code front (ascx) just put your javascript (jquery?) in there, if not then you can emit javascript to the html either manually when rendering the control (the Render event) or through ClientScript.RegisterStartupScript instead

AndreasKnudsen
+1  A: 

I prefer to put a link to the .js file associated with a specific control in the .ascx file

<script src="Js/myUserControl.js" type="text/javascript"></script>

EDIT: as a note, you can place the line above at the bottom of your .ascx file to keep the reference together with the user control - rather than put in the .aspx file.

Mark Schultheiss
I'll try that, although I don't see how linking to an external file would produce a different result that just putting the js inline. I do like that approach though
Scott
I like to keep things separate - CSS all in .css files, javascript in .js files, all the markup/structure in the ascx/aspx files, and all the classes in .cs files - over time, it makes it much easier to manage/find stuff. Ease your PAIN - Plan All Incremental Needs.
Mark Schultheiss
A: 

If you don't want to hard-code your JavaScript for start-up events, but instead include a JavaScript file:

First call ScriptManager.RegisterClientScriptInclude with the path to your script file, and then call ScriptManager.RegisterStartupScript with your start-up function in your script file.

protected void Page_Load(object sender, EventArgs e)
{
   ScriptManager.RegisterClientScriptInclude(
      this, GetType(), "formatterScript", ResolveUrl("~/js/formatter.js"));
   ScriptManager.RegisterStartupScript(
      this, GetType(), "formatTableFunction", "formatTable()", true);
}
Even Mien