What should you do?

You are implementing an ASP.NET page that includes the following drop-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 drop-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
protected void MyDropDown_PreRender(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
Label lbl = new Label();
lbl.Text = “Option”;
lbl.ID = “Option”;
ddl.Controls.Add(lbl);
}

B.
Add the following OnPreRender event handler to the asp:DropDownList
protected void MyDropDown_PreRender(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
ddl.Items.Add(“Option”);
}

C.
Add the following event handler to the page code-behind.
protected void Page_LoadComplete(object sender, EventArgs e)
{
DropDownList ddl = Page.FindControl(“MyDropDown”) as DropDownList;
Label lbl = new Label();
lbl.Text = “Option”;
lbl.ID = “Option”;
ddl.Controls.Add(lbl);
}

D.
Add the following event handler to the page code-behind.
protected void Page_LoadComplete(object sender, EventArgs e)
{
DropDownList ddl = Page.FindControl(“MyDropDown”) as DropDownList;
ddl.Items.Add(“Option”);
}



Leave a Reply 0

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