Hi @peter liles ,
If you fire the Click event, the postback is called and ASP.Net wants to fire the called event, but your controls are not added to the page, which is why ASP.Net ignores this event.
After the postback and before the event to fire, you have to add your controls again, after them, the event will be called.
I wrote a demo, you can refer to it.
<asp:Button ID="Button1" ru nat="ser ver" Text="Button1" On Click="butt on1_cl ick" /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder><br />
<asp:HiddenField runat="server" ID="controlLoaded"/>
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Button ID="Button2" run at="se rver" Text="Button2" On Click="butt on2_click" /><br />
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder><br />
<asp:HiddenField runat="server" ID="HiddenField1"/>
<asp:Label ID="Label2" runat="server" ></asp:Label>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If controlLoaded.Value = "1" Then
AddControl1()
End If
If HiddenField1.Value = "1" Then
AddControl2()
End If
End Sub
Protected Sub button1_click(ByVal sender As Object, ByVal e As EventArgs)
AddControl1()
End Sub
Protected Sub button2_click(ByVal sender As Object, ByVal e As EventArgs)
AddControl2()
End Sub
Private Sub AddControl1()
controlLoaded.Value = "1"
Dim ddList As DropDownList = New DropDownList()
ddList.ID = "ddl1"
ddList.AutoPostBack = True
ddList.Items.Add(New ListItem("Purchase", "Purchase"))
ddList.Items.Add(New ListItem("Sold", "Sold"))
AddHandler ddList.SelectedIndexChanged, AddressOf DropDownNavPathEventHandler1
PlaceHolder1.Controls.Clear()
PlaceHolder1.Controls.Add(ddList)
End Sub
Private Sub AddControl2()
HiddenField1.Value = "1"
Dim ddList As DropDownList = New DropDownList()
ddList.ID = "ddl2"
ddList.AutoPostBack = True
ddList.Items.Add(New ListItem("Purchase1", "Purchase1"))
ddList.Items.Add(New ListItem("Sold1", "Sold1"))
AddHandler ddList.SelectedIndexChanged, AddressOf DropDownNavPathEventHandler2
PlaceHolder2.Controls.Clear()
PlaceHolder2.Controls.Add(ddList)
End Sub
Private Sub DropDownNavPathEventHandler1(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "Ok1!!!"
End Sub
Private Sub DropDownNavPathEventHandler2(ByVal sender As Object, ByVal e As EventArgs)
Label2.Text = "Ok2!!!"
End Sub
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.