Executing a JS func for each user control inside a aspx page

Friday, November 21st, 2008

The scenario is, 1 aspx page needs to lead a usercontrol repetitively, and these user control needs to excute a JS method on load. The Method filters a JS array and rerenders a dropdown inside the usercontrol, so it has the reference of the server controls for respective usercontrols. thus the array is in the aspx page while the method is dynamic for each usercontrol. Now how to execute dynamic number of dynamically named functions for each usercontrol? the solution is below-

In aspx page
================================

<head runat=”server”>
<script type=”text/javascript”>
var functionList = new Array();
</script>

——–
——–

<script type=”text/javascript”>
var lenght = functionList.length;
var newfunction = functionList.pop();
while(typeof newfunction != “undefined”)
{
newfunction();
newfunction = functionList.pop();
}
</script>
</body>

In ascx page(at the end of the page)
===================

<script language=”javascript” type=”text/javascript”>
functionList.push(MethodName<%=ServerControl.ClientID %>);
</script>