• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

How do I collect data from a client-side populated dropdownlist ("select") on the server-side of an ASP.NET application?

I have an ASP.NET application that needs to collect the selected value from a dropdownlist on Submit.  The wrinkle is the dropdownlist is populated on the client-side based on selections (checkboxes) that the user makes in the browser.

On postback/submit the dropdownlist only has in it the one option I prepopulated in the page markup, not any of the options that I populated via javascript.

0
Bruce
Asked:
Bruce
  • 4
  • 3
1 Solution
 
Gibu GeorgeCTOCommented:
Please post the code
0
 
BuginatorCommented:
Is the dropdownlist asp.net control? If that's the case, use this:

String value = Request.Form[DropDownList1.ClientID];

If it is just a normal html based dropdownlist, use the following:

String value = Request.Form["theDropDownListIDName];
0
 
BruceAuthor Commented:
It is an ASP.NET server control.

I tried this...
String value = Request.Form[DropDownList1.ClientID];

But it returned null

Here is my actual code for this call:
Request.Form[ddlSelectPrimaryBusinessFunction.ClientID];

ddlSelectPrimaryBusinessFunction.ClientID does return a value.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BuginatorCommented:
Are you sure the dropdownlist contain not only the text item, but also the value?
0
 
BruceAuthor Commented:
Yes.  I am able to check the value via javascript currently.  

My current workaround for this is to add an event handler and push the selected value to a hidden field onchange.
0
 
BuginatorCommented:
I've tested this myself now, I created a dropdownlist, and a javascript that fills the ddl with a new item. I select the new item from the ddl, and press submit. On postback, the generated value is retrieved correctly. So it does work, for me. I can post the code if you want me to.

 Do you in any way modify or fill the ddl from code behind?
0
 
BruceAuthor Commented:
Cool beans Buginator!

Yes please do post the code.  I may just be having a mental block on implementation.  :-)
0
 
BuginatorCommented:
Remember to set EnableEventValidation to false, otherwise the ASP.NET engine will produce an error because it thinks something illegale has happened. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ddlformC.aspx.cs" Inherits="ddlformC" EnableEventValidation="false" %>

Alright here goes:
//PAGE:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ddlformC.aspx.cs" Inherits="ddlformC" EnableEventValidation="false" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function ddlAdd() {
        var ddl = document.getElementById('<%= DDLControl.ClientID %>');
        var opt = document.createElement("option");
                
        ddl.options.add(opt);
        opt.text = "My new text";
        opt.value = "My new value";
        alert('Item added to <%= DDLControl.ClientID %>');
        }
        </script>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
    <asp:DropDownList ID="DDLControl" runat="server">
        <asp:ListItem>Default Item</asp:ListItem>
    </asp:DropDownList>
        
     <input id="Button2" type="button" value="Add item!" onclick="ddlAdd();" /><br/>
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Get value!" />
     &nbsp;<asp:Literal ID="Literal1" runat="server"></asp:Literal>
        <br />
    </div>
    </form>
</body>
</html>
 
 
 
 
//CODE BEHIND:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class ddlformC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Literal1.Text = Request.Form[DDLControl.ClientID] ?? "Error: Null error";
    }
}

Open in new window

0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now