This is a migrated thread and some comments may be shown as answers.

Adding a RadDropDownList at runtime

1 Answer 145 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Amalie
Top achievements
Rank 1
Amalie asked on 25 Feb 2020, 08:42 AM
Is it possible to generate a RadDropDownList at runtime? Such as when a button is clicked.

1 Answer, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 03 Mar 2020, 11:49 AM

Hi Amalie,

Telerik controls are standard ASP.NET controls and the same rules apply. Creating one programmatically would be done the same way it is done using an ASP DropDownList.

ASP DropDownList Example

protected void Button1_Click(object sender, EventArgs e)
{
    DropDownList ddl = new DropDownList() { ID = "DropDownList1" };
    ddl.Items.Add("Item 1");
    ddl.Items.Add("Item 2");
    PlaceHolder1.Controls.Add(ddl);
}

 

Telerik RadDropDownList Example:

protected void RadButton1_Click(object sender, EventArgs e)
{
    RadDropDownList rddl = new RadDropDownList() { ID = "RadDropDownList1" };
    rddl.Items.Add("Item 1");
    rddl.Items.Add("Item 2");
    PlaceHolder1.Controls.Add(rddl);
}

 

The real question is, when and how to create these controls. Creating the Control on a Button click would be easy, however, it will disappear as soon as another control makes a PostBack. The answer to why can be found in the Page Life Cycle In ASP.NET article.

In order for a control to stay on the page across PostBacks, it has to be added to the ViewState. The latest event that can be used for that is the Load event of the Page, while the Button's click fires after that.

So how can it be done?

1. In the Init event of the page. Create the Object, add items to it, and add the object to the Controls collection of another control (in this example it's a PlaceHolder)

protected void Page_Init(object sender, EventArgs e)
{
    RadDropDownList rddl = new RadDropDownList() { ID = "RadDropDownList1" };
    rddl.Items.Add("Item 1");
    rddl.Items.Add("Item 2");
    PlaceHolder1.Controls.Add(rddl);
}

 

2. In the Load event of the page. In this event, the controls will be added every time the page loads, but its properties are set only once at the initial load. The ViewState will carry over this information across multiple PostBacks.

protected void Page_Load(object sender, EventArgs e)
{
    RadDropDownList rddl = new RadDropDownList() { ID = "RadDropDownList1" };
    PlaceHolder1.Controls.Add(rddl);

    if (!IsPostBack)
    {
        rddl.Items.Add("Item 1");
        rddl.Items.Add("Item 2");
    }
}

 

Yes, but how it is done on a Button's click event?

It's simple. Create a control with its visibility set to False by default then add it to the page at either Init or Load events like the examples show it above. On Button click, make the control visible.

protected void Page_Load(object sender, EventArgs e)
{
    RadDropDownList rddl = new RadDropDownList() { ID = "RadDropDownList1" };
    PlaceHolder1.Controls.Add(rddl);

    if (!IsPostBack)
    {
        rddl.Items.Add("Item 1");
        rddl.Items.Add("Item 2");
        rddl.Visible = false;
    }
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    RadDropDownList rddl = PlaceHolder1.FindControl("RadDropDownList1") as RadDropDownList;
    rddl.Visible = true;
}

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Tags
General Discussions
Asked by
Amalie
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or