以编程方式将动态AJAX控件添加到ASP页面

时间:2012-01-25 12:55:34

标签: asp.net ajax vb.net

是否可以以编程方式将动态AJAX控件添加到ASP页面?

现在我在Page_Init Event中动态创建Textbox,Listbox。我有一个悬停菜单,只附加到第一个ListBox。

我需要将它添加到创建其他控件的循环中,以便它可以在每个List框中使用。

以下是我的一些代码......

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" & I.ToString("D2") 
   ph.Controls.Add(txtD) 

   Dim litCtrlD As New Literal 
   litCtrlD.ID = "litCtrlD" & I.ToString("D2") 
   litCtrlD.Text = "<br />" 
   ph.Controls.Add(litCtrlD) 

   txtD.Text = "ENGLISH" 

   Dim txtA As New TextBox 
   txtA.ID = "txtAEdit" & I.ToString("D2") 
   ph.Controls.Add(txtA) 

   Dim litCtrla As New Literal 
   litCtrla.ID = "litCtrla" & I.ToString("D2") 
   litCtrla.Text = "<br />" 
   ph.Controls.Add(litCtrla) 

   Dim txtE As New TextBox 
   txtE.ID = "txtEEdit" & I.ToString("D2") 
   ph.Controls.Add(txtE) 

   Dim litCtrlE As New Literal 
   litCtrlE.ID = "litCtrlE" & I.ToString("D2") 
   litCtrlE.Text = "<br />" 
   ph.Controls.Add(litCtrlE) 

   txtE.Text = "TRANSLATION" 

   Dim txtB As New TextBox 
   txtB.ID = "txtBEdit" & I.ToString("D2") 
   ph.Controls.Add(txtB) 

   AddHandler txtB.TextChanged, AddressOf txtBChanged 

   Dim litCtrl As New Literal 
   litCtrl.ID = "litCtrl" & I.ToString("D2") 
   ph.Controls.Add(litCtrl) 

   Dim lstF As New ListBox 
   lstF.ID = "lstFEdit" & I.ToString("D2") 
   ph.Controls.Add(lstF) 

   Dim litCtrlF As New Literal 
   litCtrlF.ID = "litCtrlF" & I.ToString("D2") 
   ph.Controls.Add(litCtrlF) 

   Dim lstG As New ListBox 
   lstG.ID = "lstGEdit" & I.ToString("D2") 
   ph.Controls.Add(lstG) 

   AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged 

   Dim litCtrlG As New Literal 
   litCtrlG.ID = "litCtrlG" & I.ToString("D2") 
   ph.Controls.Add(litCtrlG) 

Next I 

我需要在此代码区域的某处添加悬停菜单。

Dim lstG As New ListBox
lstG.ID = "lstGEdit" & I.ToString("D2")
ph.Controls.Add(lstG)

AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged

Dim litCtrlG As New Literal
litCtrlG.ID = "litCtrlG" & I.ToString("D2")
ph.Controls.Add(litCtrlG)

感谢有关这方面的帮助。

编辑:我已将以下代码添加到Page_Init事件以尝试构建悬停菜单,但收到错误,如下所述....

' ************************************
' Creating Hover
  Dim hoverMenuExt As New AjaxControlToolkit.HoverMenuExtender
  hoverMenuExt.ID = "hovExtEdit" & I.ToString("D2")     **
  ph.Controls.Add(hoverMenuExt)

  Dim litHoverMenuExt As New Literal
  litHoverMenuExt.ID = "litHoverMenuExt" & I.ToString("D2")
  ph.Controls.Add(litHoverMenuExt)

**有错误的行:在为其指定值之前使用变量“hoverMenuExt.ID”。在运行时可能会产生空引用异常。

这方面有什么想法吗?

1 个答案:

答案 0 :(得分:0)

仅供参考,这是一个很棒的链接,展示了如何动态添加标准控件。

Click here.

这提供了对动态控制的深入回顾,详细说明了这里的细节。