title: Calling user control javascript functions from a new window (where the user controls are in the opener)
body: <p>I have a user control which opens a new window for some item selection.
  Once an item is selected on that window, the item ID should be passed to a callback javascript function in the user control.</p>
  
  <p>In order to have a unique callback function name in the user control (it may have multiple instances on the same page), I added the user control ID to the function name as follows (defined in the ascx file):</p>
  
  <pre><code>function OnItemSelection_&lt;%=this.ID%&gt;(selectedItemID)
  {
      OnItemSelection("&lt;%=SomeControl.ClientID%&gt;", selectedItemID)
  }
  </code></pre>
  
  <p>Where OnItemSelection is defined in an external js file:</p>
  
  <pre><code>function OnItemSelection(controlID, selectedItemID)
  {
      $(controlID).do_something(selectedItemID);
  }
  </code></pre>
  
  <p>The issue is that now I'm not sure how the new page can call the "right" function (of the user control instance where it was opened from).</p>
  
  <p>The only idea I have in mind so far is passing the name of the callback function in the query string:</p>
  
  <pre><code>var CallbackFunction = function(selectedItemID)
  {
      window.opener.&lt;%=Request.QueryString["CallbackFunctionName"]%&gt;(selectedItemID);
  }
  </code></pre>
  
  <p>or maybe just the opening user control ID:</p>
  
  <pre><code>var CallbackFunction = function(selectedItemID)
  {
      window.opener.OnItemSelection_&lt;%=Request.QueryString["UserControlID"]%&gt;(selectedItemID);
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <p>1) Is it a bad practice passing javascript function names (or user control IDs) like that (I suspect it is)?</p>
  
  <p>2) What could be better way to handle this scenario?</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>Another way I'm considering now is declaring a global variable in the user control (registering it from the code behind so it'll appear only once). Then, when opening the new window, it's possible to assign the relevant function to it (OnItemSelection_&lt;%=this.ID%>), and call it from the new window.</p>
  
  <p>Seems much cleaner than passing user control data through the query string.</p>