question

peterliles-4155 avatar image
0 Votes"
peterliles-4155 asked LanHuang-MSFT commented

Initialise a new dropdownlist control

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


dotnet-aspnet-webpages
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Dynamically created server controls are not members of the Page class and therefore not part of Web Forms default state management. It is up to you to recreate the controls early in the page life cycle if you want access to a control's value in event handlers that fire later. Usually the code is within the Page_Init but the Page_Load can work. This might mean you must write code to maintain state too. For example which dropdowns are active during a post back. You can always get the value using Request["NameOfTheControl"] if you don't care about rendering the server control. again.

However, there is usually no need to create dynamic controls in Web Forms because the data bound controls handle dynamic rendering.

Anyway, this is a very common situation in Web Forms and well documented. Do a Google Search.

0 Votes 0 ·

However, there is usually no need to create dynamic controls in Web Forms because the data bound controls handle dynamic rendering

I have several buttons on page that when clicked should display a different dropdown content while other buttons dont display anything. and i dont want a dropdown control rendered on page when not necessary. Thats why i require to create dynamic dropdownlist.

0 Votes 0 ·

Hi @peterliles-4155,
The previous demo only wrote a button, I modified my demo according to your needs, I hope it will help you.
Best regards,
Lan Huang

0 Votes 0 ·

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT edited

Hi @peterliles-4155,
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.



qwe.gif (63.6 KiB)
7788.gif (121.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.