Solved

dynamically add label and dropdownlist

Posted on 2009-06-30
3
849 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now