You need to dynamically add values to the end of the drop-down list

You are implementing an ASP.NET page that includes the following down list.
<asp:PlaceHolder ID=”dynamicControls” runat=”server”>
<asp:DropDownList ID=”MyDropDown” runat=”server”>
<asp:ListItem Text=”abc” value=”abc” />
<asp:ListItem Text=”def” value=”def” />
</asp:DropDownList>
</asp:PlaceHolder>
You need to dynamically add values to the end of the drop-down list. What should you do?

You are implementing an ASP.NET page that includes the following down list.
<asp:PlaceHolder ID=”dynamicControls” runat=”server”>
<asp:DropDownList ID=”MyDropDown” runat=”server”>
<asp:ListItem Text=”abc” value=”abc” />
<asp:ListItem Text=”def” value=”def” />
</asp:DropDownList>
</asp:PlaceHolder>
You need to dynamically add values to the end of the drop-down list. What should you do?

A.
Add the following OnPreRender event handler to the asp:DropDownList
Private Sub mydropdown_PreRender(ByVal sender As Object, ByVal e As System.
EventArgs) Handles mydropdown.PreRender
Dim ddl As DropDownList = TryCast(sender, DropDownList)
Dim lbl As Label = New Label()
lbl.Text = “Option”
lbl.ID = “Option”
ddl.Controls.Add(lbl)
End Sub

B.
Add the following OnPreRender event handler to the asp:DropDownList
Private Sub mydropdown_PreRender(ByVal sender As Object, ByVal e As System.
EventArgs) Handles mydropdown.PreRender

Dim ddl As DropDownList = TryCast(sender, DropDownList)
ddl.Items.Add(“Option”)
End Sub

C.
Add the following event handler to the page code-behind.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim ddl As DropDownList = TryCast(Page.FindControl(“mydropdown”),
DropDownList)
Dim lbl As Label = New Label()
lbl.Text = “Option”
lbl.ID = “Option”
ddl.Controls.Add(lbl)
End Sub

D.
Add the following event handler to the page code-behind.
Private Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.
EventArgs) Handles Me.LoadComplete
TryCast(Page.FindControl(“mydropdown”), DropDownList)
ddl.Items.Add(“Option”)
End Sub



Leave a Reply 1

Your email address will not be published. Required fields are marked *