Solved

dynamically add label and dropdownlist

Posted on 2009-06-30
3
850 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
  • 2
3 Comments
 
LVL 9

Expert Comment

by:sevensnake77
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:
sevensnake77 earned 250 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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now