dynamically add label and dropdownlist

I would like to create dynamically rows with a label and a dropdownlist. The number of the rows varies and depends on data from my database.
How can I do this?
Who is Participating?
Seven priceConnect With a Mentor Full StackCommented:
or with javascript try to use Dom elements.

var dd = document.createElement("select");
dd.name = "name";
dd.id = "id";
dd.options[dd.length] = new Option("text", "value");
dd.options[dd.length] = new Option("text", "value");
dd.options[dd.length] = new Option("text", "value");

//Add the dropdown to the parent node
Seven priceFull StackCommented:
maybe something like this, Not to sure what you meant.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)

protected void Page_Init(object sender, EventArgs e)

        SqlDataSource sqlDS = new SqlDataSource();
        sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings[0].ToString();
        sqlDS.SelectCommand = "select GenderID,Gender from mylookupGender";

        DropDownList ddl = new DropDownList();
        ddl.ID = "dddlGender";
        ddl.DataSource = sqlDS;
        ddl.DataTextField = "Gender";
        ddl.DataValueField = "GenderID";

        // ... Repeat above code 9 times or put in a for loop if they're all the same...

private void BindDropDownLists()
    foreach (Control ctl in form1.Controls)
        if (ctl is DropDownList)
            (ctl as DropDownList).DataBind();

Seo_ExpertConnect With a Mentor Commented:
You can create function which will add new rows to a table and assign table ID

function addRowToTable()
var tbl = document.getElementById('tblIssue');
var lastRow = tbl.rows.length;
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
var cellLeft = row.insertCell(0);
//var textNode = document.createTextNode(iteration);
var textNode = document.createTextNode("Lable text");
var cellTwo = row.insertCell(1);
var ta = document.createElement('select');
ta.name = 'txtDetails' + iteration;
ta.id = 'txtDetails' + iteration;
ta.options[dd.length] = new Option("text", "value");
ta.options[dd.length] = new Option("text", "value");
ta.options[dd.length] = new Option("text", "value");

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.