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 the jQuery library 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?
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.
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.
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.
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.
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
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);
}
精彩评论