title: programmatically add dynamic AJAX controls to an ASP page
body: <p>Is it possible to programmatically add dynamic AJAX controls to an ASP page?</p>
  
  <p>Now I create Textbox, Listbox dynamically in Page_Init Event. I have a Hover Menu that only attaches to the first ListBox.</p>
  
  <p>I need to add it to the loop that creates the other controls so that it will be available on each List box.</p>
  
  <p>Here is some of my code...</p>
  
  <pre><code>Dim num As Integer = Session("lineNums")     ' Determines how many text boxes need to be created based on carriage returns from textblock. 
  
  Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder) 
  
  Dim ph As PlaceHolder = DirectCast(MainContent.FindControl("PlaceHolder1"), PlaceHolder) 
  
  For I = 1 To num 
  
     Dim txtD As New TextBox 
     txtD.ID = "txtDEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(txtD) 
  
     Dim litCtrlD As New Literal 
     litCtrlD.ID = "litCtrlD" &amp; I.ToString("D2") 
     litCtrlD.Text = "&lt;br /&gt;" 
     ph.Controls.Add(litCtrlD) 
  
     txtD.Text = "ENGLISH" 
  
     Dim txtA As New TextBox 
     txtA.ID = "txtAEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(txtA) 
  
     Dim litCtrla As New Literal 
     litCtrla.ID = "litCtrla" &amp; I.ToString("D2") 
     litCtrla.Text = "&lt;br /&gt;" 
     ph.Controls.Add(litCtrla) 
  
     Dim txtE As New TextBox 
     txtE.ID = "txtEEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(txtE) 
  
     Dim litCtrlE As New Literal 
     litCtrlE.ID = "litCtrlE" &amp; I.ToString("D2") 
     litCtrlE.Text = "&lt;br /&gt;" 
     ph.Controls.Add(litCtrlE) 
  
     txtE.Text = "TRANSLATION" 
  
     Dim txtB As New TextBox 
     txtB.ID = "txtBEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(txtB) 
  
     AddHandler txtB.TextChanged, AddressOf txtBChanged 
  
     Dim litCtrl As New Literal 
     litCtrl.ID = "litCtrl" &amp; I.ToString("D2") 
     ph.Controls.Add(litCtrl) 
  
     Dim lstF As New ListBox 
     lstF.ID = "lstFEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(lstF) 
  
     Dim litCtrlF As New Literal 
     litCtrlF.ID = "litCtrlF" &amp; I.ToString("D2") 
     ph.Controls.Add(litCtrlF) 
  
     Dim lstG As New ListBox 
     lstG.ID = "lstGEdit" &amp; I.ToString("D2") 
     ph.Controls.Add(lstG) 
  
     AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged 
  
     Dim litCtrlG As New Literal 
     litCtrlG.ID = "litCtrlG" &amp; I.ToString("D2") 
     ph.Controls.Add(litCtrlG) 
  
  Next I 
  </code></pre>
  
  <p>I need to add the Hover Menu somewhere in this code area.</p>
  
  <pre><code>Dim lstG As New ListBox
  lstG.ID = "lstGEdit" &amp; I.ToString("D2")
  ph.Controls.Add(lstG)
  
  AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged
  
  Dim litCtrlG As New Literal
  litCtrlG.ID = "litCtrlG" &amp; I.ToString("D2")
  ph.Controls.Add(litCtrlG)
  </code></pre>
  
  <p>Thanks forany help on this one.</p>
  
  <p><strong>EDIT</strong>: I have added the following code to the Page_Init Event to try and build the Hover menus but am getting an error as described below....</p>
  
  <pre><code>' ************************************
  ' Creating Hover
    Dim hoverMenuExt As New AjaxControlToolkit.HoverMenuExtender
    hoverMenuExt.ID = "hovExtEdit" &amp; I.ToString("D2")     **
    ph.Controls.Add(hoverMenuExt)
  
    Dim litHoverMenuExt As New Literal
    litHoverMenuExt.ID = "litHoverMenuExt" &amp; I.ToString("D2")
    ph.Controls.Add(litHoverMenuExt)
  </code></pre>
  
  <p>** Line with error: Variable "hoverMenuExt.ID" is used before it has been assigned a value. A null reference exception could result at runtime.</p>
  
  <p>Any ideas on this part?</p>