In ASP.Net I have a few custom controls I've made. I utilized jQuery where it helped also. Well, one problem I have now(with obvious, but "bad" workarounds) is that for each user control I need to execute some code from within pageLoad
($(document).ready
will not work with update panels).
Well so now my problem. I need to have two custom controls attach to the pageLoad
event.
What would be the best way of doing this?
I can't just do
old_pageLoad=pageLoad
pageLoad=function(){... old_pageLoad();}
because these custom controls can be used more than once on a page and the script needs to run for every single instance of the control, plus what if I had 3 different custom controls on the page?
the only method I've come up with is something like this which seems super hackish:
old_pageLoad_<%= MyStaticClass.GetUniqueID() %>=pageLoad;
pageLoad=function(){... old_pageLoad_<%= MyStaticClass.GetUniqueID() %>();}
Are there any better ways of handling function conflicts like this?
I have also seen this MSDN article but what it suggests doing seems worse to me than what I am currently doing.
The basic idea is that of having a control (that is a combination of different HTML tags, styles, css and event handlers) written in JavaScript that will be able to add himself to a web page or a portion of it with all his logic inside of it.
Controls are useful when you're developing complex applications because you can avoid code duplication. Because custom controls are objects, you can use the typical features offered by OOP. You can start from scratch with your own control, or use an existing control and enrich it.
Uploading the JS FileIn the Create Form dialog, choose the JavaScript Control menu, and select Add Action Control. In the Add Javascript Control window, name the control. Ensure the Control Type is set to Action. This control has no input data; therefore, nothing needs to be added to the Input Data section.
JavaScript. Control structures. Functions. The control structures within JavaScript allow the program flow to change within a unit of code or function. These statements can determine whether or not given statements are executed - and provide the basis for the repeated execution of a block of code.
Use the RegisterStartupScript method of the ScriptManager
. You pass in a reference to your control while registering the script, and that ensures that the initialization script is ran whenever the control is updated (either on the first page load, or when an UpdatePanel is updated).
Another way would be to add a handler that calls a Javascript method when an async postback ends. I've done this on several occasions to re-initialise jQuery dialogs. E.g.
$().ready(function() {
myInitMethod();
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myInitMethod);
});
function myInitMethod() }
// Init code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With