0

I need to generate 3 DropDownList Boxes in a row Dynamically on ADD Button click event. Once the DDL is generated i should be able to assign Data source to it and also do DDL-selectedEventChanged functionality on them in c# asp.net.
Below link was exactly am looking for but i couldn't assign data source or i couldn't do any functionality

"Adding multiple DropDownLists with a button click"

any better Ideas please help

if i press Button i need to get 3 DDLs at a time, if i press again it should generate again, so number of clicks= no. of rows with three DDL's in each row

5
  • Might be easier if the DDL's existed but weren't visible until the button was clicked. Commented Feb 26, 2013 at 17:16
  • @SteveWash I agree that it is easy to implement, but the drawback is ViewState will be heavy with unnecessary controls. Commented Feb 26, 2013 at 17:20
  • @shiva kumar - How do you want to populate those DropDownLists with data? In other words, where are you going to get the data from? Commented Feb 26, 2013 at 17:21
  • okay, yeah but the problem is we are not supposed to restrict user from selecting no. of DDL rows, so if i use about 10 then also it may not help. still can try though for the moment, but i was expecting help for C# Code atleast for future Use, and ive seen use of this scenario so many times, yet can be achieved by JQuery. Commented Feb 26, 2013 at 17:23
  • My app is nothing but Adhoc SQL Generator, so i will get tables from any database where a user installs this app. After selecting Tables these newly generated DDLs will load values in it, and user can select any value from the newly generated DDLs. and based on the selected value i will generate SQL Query. Commented Feb 26, 2013 at 17:29

1 Answer 1

1

Here is the sample code. The only trick is that you need to load those dynamic controls again after post back. Otherwise, you won't be able to access those.

enter image description here

<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>
<asp:Button runat="server" ID="AddButton" OnClick="AddButton_Click" Text="Add" />

private List<int> _controlIds;

private List<int> ControlIds
{
    get
    {
        if (_controlIds == null)
        {
            if (ViewState["ControlIds"] != null)
                _controlIds = (List<int>)ViewState["ControlIds"];
            else
                _controlIds = new List<int>();
        }
        return _controlIds;
    }
    set { ViewState["ControlIds"] = value; }
}

private List<string> DataSource
{
    get { return new List<string> { "One", "Two", "Three" }; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        var dataSource = DataSource;

        foreach (int id in ControlIds)
        {
            var ddl = new DropDownList();
            ddl.ID = id.ToString();

            foreach (var data in dataSource)
                ddl.Items.Add(new ListItem(data));

            PlaceHolder1.Controls.Add(ddl);
        }
    }
}

protected void AddButton_Click(object sender, EventArgs e)
{
    var dataSource = DataSource;
    for (int i = 0; i < 3; i++)
    {
        var reqs = ControlIds;
        int id = ControlIds.Count + 1;

        reqs.Add(id);
        ControlIds = reqs;

        var ddl = new DropDownList();
        ddl.ID = id.ToString();

        foreach (var data in dataSource)
            ddl.Items.Add(new ListItem(data));

        PlaceHolder1.Controls.Add(ddl);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you,This works perfect, i need some workouts to assign my dataset to List and then to DDLs, can you help me how to add Dataset to Datasource list method. i have achieved it on button click but for second click on pageload how do we do it...please help
Sure we can help you. Please create a new answer if you cannot find what you are look for in stackoverflow.
Modified the datasource to perform an sql query and populate and this works perfectly for my purposes. Not overly keen on storing in the viewstate but I have a time constraint. +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.