Hi
I know dynamic dropdownlist in PHP but I want to know how to create dynamic dropdownlist in .NET ? Please tell me anyone with an example ?
Thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 9, 2012, 6:26 PM EDT
1. Create dropdownlist dynamically with autopostback property true
2. Keep dropdownlist during postback
3. Maintain selected value during postback.
Here is the sample for this.
In aspx page:
To Create Dynamic Dropdownlist:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CreateDynamicTable();
}
}
private void CreateDynamicTable()
{
// Fetch the number of Rows and Columns for the table
// using the properties
int tblRows = 3;
int tblCols = 3;
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
DropDownList ddl = new DropDownList();
ddl.ID = "ddl-" + i.ToString() + "-" + j.ToString();
for (int k = 0; k < 5; k++)
{
ddl.Items.Add(new ListItem("item-"+k.ToString(),k.ToString()));
}
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
// Add the control to the TableCell
tc.Controls.Add(ddl);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
tbl.EnableViewState = true;
ViewState["tbl"] = true;
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
// what you want to perform
}
To retrieve value on button click:
protected void btnSet_Click(object sender, EventArgs e)
{
foreach (TableRow tr in tbl.Controls )
{
foreach (TableCell tc in tr.Controls)
{
if (tc.Controls[0] is DropDownList)
{
Response.Write(((DropDownList)tc.Controls[0]).SelectedItem.Text+" ");
}
}
Response.Write("
");
}
}
Right Now, No output because dynamic controls are lost in postback then what to do.
So we need to save dynamic controls value and generate dynamic controls again.we need to maintain viewstate.
protected override object SaveViewState()
{
object[] newViewState = new object[2];
List
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is DropDownList)
{
txtValues.Add(((DropDownList)cell.Controls[0]).SelectedIndex.ToString());
}
}
}
newViewState[0] = txtValues.ToArray();
newViewState[1] = base.SaveViewState();
return newViewState;
}
protected override void LoadViewState(object savedState)
{
//if we can identify the custom view state as defined in the override for SaveViewState
if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
{
object[] newViewState = (object[])savedState;
string[] txtValues = (string[])(newViewState[0]);
if (txtValues.Length > 0)
{
//re-load tables
CreateDynamicTable();
int i = 0;
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is DropDownList && i < txtValues.Length)
{
((DropDownList)cell.Controls[0]).SelectedIndex = Convert.ToInt32(txtValues[i++]);
}
}
}
}
//load the ViewState normally
base.LoadViewState(newViewState[1]);
}
else
{
base.LoadViewState(savedState);
}
}
Please refer the below link
http://techbrij.com/200/create-dynamic-dropdownlists-in-asp-net
http://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx
Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted Apr 9, 2012, 7:23 PM EDT
please refer,
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/dynamically-adding-dropdown-using-jquery/
http://stackoverflow.com/questions/5458231/dynamically-add-drop-down-lists-to-html-form
this will help you to create drop down client side.
hope this help.