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
Solved

dynamically add label and dropdownlist

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
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)

839 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