Ebbene, il codice di sotto fa quello che chiede andgio76.
Chiaramente è un po' diverso ed è scritto in basic, ma quello che conta è scoprire il principio per poi tradurlo secondo il proprio caso.
E' un ListBox. Cliccando su un elemento si crea un altro ListBox, dinamicamente, con esso pure un evento per il click
codice:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AggiuntaDinamicaControlliListBox.aspx.vb" Inherits="CorsoApogeo_AggiuntaDinamicaControlliListBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Pagina senza titolo</title>
<link href="../stili/Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h3>Aggiunta di ListBox da codice con AutoPostBack</h3>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text=" " onmouseover="this.style.color='red';" onmouseout="this.style.color='black';"></asp:Label>
<hr />
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true">
<asp:ListItem Value="a" Text="a"></asp:ListItem>
<asp:ListItem Value="b" Text="b"></asp:ListItem>
<asp:ListItem Value="c" Text="c"></asp:ListItem>
</asp:ListBox>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<hr />
<asp:LinkButton ID="LinkButton1" runat="server">Refresh</asp:LinkButton>
</form>
</body>
</html>
---------------------------------------------------------------------------------------------------------
Option Strict On
Partial Class CorsoApogeo_AggiuntaDinamicaControlliListBox
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Me.IsPostBack Then
If Not (Me.ViewState("SelectedValue") Is Nothing) Then
CreaListBox(Me.ViewState("SelectedValue").ToString())
End If
End If
End Sub
Protected Sub ListBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.TextChanged
Dim lb As ListBox = DirectCast(sender, ListBox)
If lb.ID = "ListBox1" Then
CreaListBox(lb.SelectedValue)
End If
Me.Label1.Text = lb.ID & " = " & lb.SelectedValue
End Sub
Private Sub CreaListBox(ByVal SelectedValue As String)
Me.ViewState("SelectedValue") = SelectedValue
Dim lb As New ListBox()
With lb
lb.ID = "ListBox2"
lb.AutoPostBack = True
End With
Select Case SelectedValue
Case "a"
lb.Items.Add(New ListItem("a1", "a1"))
lb.Items.Add(New ListItem("a2", "a2"))
lb.Items.Add(New ListItem("a3", "a3"))
Case "b"
lb.Items.Add(New ListItem("b1", "b1"))
lb.Items.Add(New ListItem("b2", "b2"))
lb.Items.Add(New ListItem("b3", "b3"))
Case "c"
lb.Items.Add(New ListItem("c1", "c1"))
lb.Items.Add(New ListItem("c2", "c2"))
lb.Items.Add(New ListItem("c3", "c3"))
End Select
AddHandler lb.TextChanged, AddressOf ListBox1_TextChanged
Me.PlaceHolder1.Controls.Clear()
Me.PlaceHolder1.Controls.Add(lb)
End Sub
End Class