Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way of handling javascript within Custom Controls?

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.

like image 213
Earlz Avatar asked Jul 27 '10 23:07

Earlz


People also ask

What is Custom Control in JavaScript?

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.

Why would you use custom controls?

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.

How do you use controls in JavaScript?

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.

What are JavaScript controls?

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.


2 Answers

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

like image 98
Matti Virkkunen Avatar answered Oct 05 '22 22:10

Matti Virkkunen


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
}
like image 24
Phil Hale Avatar answered Oct 05 '22 23:10

Phil Hale