ASP.net C# Read LiteralControl <select> <Option> from code behind

in My ASP code I create a Dropdown using

c# :

DivPlant.Controls.Add(new LiteralControl("<select id='" + SelectID + "' multiple='multiple'>"+Options+"</select>"));

ASPX:

 <div id="DivPlant" runat="server" multiple="multiple" style="float:left">

How do i get the selected values from code behind ?

Answers 1

  • You can access the selected value using Request.Form[]

    In you aspx page:

    <form id="form1" runat="server">
    
    <div id="DivPlant" runat="server" style="float:left">
    <asp:Label runat="server" id="lblSelection"></asp:Label>
    <asp:Button id="btnSubmit" runat="server" onclick="btnSubmit_Click"/>    
    </div>
    </form>
    

    In you code behind:

    protected void Page_Load(object sender, EventArgs e)
        {
            string SelectID = "ddlTest";
            string Options = "<option value='volvo'>Volvo</option>"; 
            DivPlant.Controls.Add(new LiteralControl("<select name='ddlName' id='" + SelectID + "'>" + Options + "</select>"));
    
        }
    
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblSelection.Text = Request.Form["ddlName"];
    
        }
    

Related Articles