Solved

dynamically add label and dropdownlist

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

705 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