?
Solved

dynamically add label and dropdownlist

Posted on 2009-06-30
3
Medium Priority
?
862 Views
Last Modified: 2013-11-11
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?
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Seven price
ID: 24749244
maybe something like this, Not to sure what you meant.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDropDownLists();
    }
}

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";
        form1.Controls.Add(sqlDS);

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

        // ... 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();
        }
    }
}


0
 
LVL 9

Accepted Solution

by:
Seven price earned 1000 total points
ID: 24749268
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
el.appendChild(dd);
0
 
LVL 7

Assisted Solution

by:Seo_Expert
Seo_Expert earned 1000 total points
ID: 24753164
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");
cellLeft.appendChild(textNode);
 
var cellTwo = row.insertCell(1);
var ta = document.createElement('select');
 
ta.name = 'txtDetails' + iteration;
ta.id = 'txtDetails' + iteration;
ta.className='pms_textbox';
ta.options[dd.length] = new Option("text", "value");
ta.options[dd.length] = new Option("text", "value");
ta.options[dd.length] = new Option("text", "value");
 
cellTwo.appendChild(ta);
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question