Get dynamically generated ASP.Net DropDownList selected value on Button click using C# and VB.Net

Last Reply one year ago By kalpesh

Posted one year ago

How i get dynamic dropdown selected value while i click on button in asp.net.

if (FieldType == "Select")
{
    int index = pnlTextBoxes.Controls.OfType<DropDownList>().ToList().Count + 1;
    DropDownList ddllst = new DropDownList();

    ddllst.ID = "ddlDynamic" + index;

    ddllst.CssClass = "table-group-action-input form-control input-medium";

    DataSet ds2 = new DataSet();
    ds2 = obj.GetDynamicInvoiceType(objdata);

    if (ds2.Tables[0].Rows.Count > 0)
    {
        ddllst.DataSource = ds2.Tables[0];
        ddllst.DataTextField = "InvoiceType";
        ddllst.DataValueField = "InvoiceTypeId";
        ddllst.DataBind();
        ddllst.Items.Insert(0, new ListItem("--Select--"));
    }
    else
    {
        ddllst.DataSource = null;
        ddllst.DataBind();
        ddllst.Items.Insert(0, new ListItem("--Select--"));
    }

    pnlTextBoxes.Controls.Add(ddllst);
    pnlTextBoxes.Controls.Add(new LiteralControl("<br />"));
}

so how can i  get this dropdown value while i click on button.

Posted one year ago

Refer the below sample code and implement it as per your code logic.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        input[type=text]
        {
            margin-bottom: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="pnlDropDowns" runat="server">
    </asp:Panel>
    <hr />
    <asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddDropDownList" />
    <asp:Button ID="btnGet" runat="server" Text="Get Values" OnClick="GetDropDownListValues" />
    </form>
</body>
</html>

C#

protected void Page_PreInit(object sender, EventArgs e)
{
    List<string> DropDownkeys = Request.Form.AllKeys.Where(key => key.Contains("ddlDynamic")).ToList();
    int i = 1;
    foreach (string key in DropDownkeys)
    {
        this.CreateTextBox("ddlDynamic" + i, i);
        i++;
    }
}

protected void AddDropDownList(object sender, EventArgs e)
{
    int index = pnlDropDowns.Controls.OfType<DropDownList>().ToList().Count + 1;
    this.CreateTextBox("ddlDynamic" + index, index);
}

private void CreateTextBox(string id, int index)
{
    DropDownList ddllst = new DropDownList();
    ddllst.ID = id;
    DataSet ds2 = new DataSet();
    ds2 = Getdata();
    if (ds2.Tables[0].Rows.Count > 0)
    {
        ddllst.DataSource = ds2.Tables[0];
        ddllst.DataTextField = "Name";
        ddllst.DataValueField = "Id";
        ddllst.DataBind();
        ddllst.Items.Insert(0, new ListItem("--Select--"));
    }
    else
    {
        ddllst.DataSource = null;
        ddllst.DataBind();
        ddllst.Items.Insert(0, new ListItem("--Select--"));
    }

    pnlDropDowns.Controls.Add(ddllst);
    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlDropDowns.Controls.Add(lt);
}

protected void GetDropDownListValues(object sender, EventArgs e)
{
    string message = "";
    foreach (DropDownList dropDownList in pnlDropDowns.Controls.OfType<DropDownList>())
    {
        message += dropDownList.ID + ": " + " Text :" + dropDownList.SelectedItem.Text + " value :" + dropDownList.SelectedItem.Value + "\\n";
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}

private DataSet Getdata()
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2]
    { 
        new DataColumn ("Id",typeof(string)),
        new DataColumn ("Name",typeof(string))
            });

    dt.Rows.Add("1", "Test 1");
    dt.Rows.Add("2", "Test 2");
    dt.Rows.Add("3", "Test 3");
    dt.Rows.Add("4", "Test 4");
    ds.Tables.Add(dt);
    return ds;
}

VB.Net

Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
    Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("ddlDynamic")).ToList()
    Dim i As Integer = 1
    For Each key As String In keys
        Me.CreateDropDownList("ddlDynamic" & i, i)
        i += 1
    Next
End Sub

Protected Sub AddDropDownList(sender As Object, e As EventArgs)
    Dim index As Integer = pnlDropDowns.Controls.OfType(Of DropDownList)().ToList().Count + 1
    Me.CreateDropDownList("ddlDynamic" & index, index)
End Sub

Private Sub CreateDropDownList(id As String, index As Integer)
    Dim ddllst As New DropDownList()
    ddllst.ID = id
    Dim ds = New DataSet()
    ds = GetData()
    If ds.Tables(0).Rows.Count > 0 Then
        ddllst.DataSource = ds.Tables(0)
        ddllst.DataTextField = "Name"
        ddllst.DataValueField = "Id"
        ddllst.DataBind()
        ddllst.Items.Insert(0, New ListItem("--Select--"))
    Else
        ddllst.DataSource = Nothing
        ddllst.DataBind()
        ddllst.Items.Insert(0, New ListItem("--Select--"))
    End If

    pnlDropDowns.Controls.Add(ddllst)
    Dim lt As New Literal()
    lt.Text = "<br />"
    pnlDropDowns.Controls.Add(lt)
End Sub

Protected Sub GetDropDownListValues(sender As Object, e As EventArgs)
    Dim message As String = ""
    For Each dropDownList As DropDownList In pnlDropDowns.Controls.OfType(Of DropDownList)()
        message += dropDownList.ID + ": " + " Text :" + dropDownList.SelectedItem.Text + " value : " + dropDownList.SelectedItem.Value + "\n"
    Next
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub


Private Function GetData() As DataSet
    Dim ds As DataSet = New DataSet()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Id", GetType(String)), New DataColumn("Name", GetType(String))})
    dt.Rows.Add("1", "Test 1")
    dt.Rows.Add("2", "Test 2")
    dt.Rows.Add("3", "Test 3")
    dt.Rows.Add("4", "Test 4")
    ds.Tables.Add(dt)
    Return ds
End Function

Screenshot