Initialise a new dropdownlist control

peter liles 556 Reputation points
2022-07-27T13:38:19.92+00:00

I have several button with some that create a new dropdownlist on click event. If i place it in the button event the dropdownlist handler wont work when a button is clicked.
So i added them to the page init event handler instead.
The problem now is i only want to create dropdownlist dependant on which button is clicked, not every postedback when any button is clicked.
Also, i want to access the selected dropdownlist item during another button click event i.e delete operation.

Code

Private Sub MyProject_AdminManager___Copy_Messaging_FileCleanUp_Init(sender As Object, e As EventArgs) Handles Me.Init  
    Dim ddList As DropDownList = New DropDownList()  
    ddList.ID = "ddl"  
    ddList.AutoPostBack = True  
    ddList.Attributes.Add("runat", "server")  

ddList.Items.Add(New ListItem("Purchase", "Purchase"))
ddList.Items.Add(New ListItem("Sold", "Sold"))

AddHandler ddList.SelectedIndexChanged, AddressOf DropDownNavPathEventHandler
PlaceHolder1.Controls.Add(ddList)
End Sub

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,292 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,956 Reputation points Microsoft Vendor
    2022-07-28T09:35:17.597+00:00

    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  
    

    226006-7788.gif
    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.

    1 person found this answer helpful.
    0 comments No comments