简体   繁体   中英

How do I get selected value from select box using Request.Form?

I'm submitting a form and need to collect the data.
Following this example , I'm trying to retrieve the value I selected in a select box.

The problem is, the select box does not have the attribute 'name'.

<asp:DropDownList runat="server" ID="countySelect" CssClass="ddlCountySelect" DataValueField="kommunekode" DataTextField="kommune" ></asp:DropDownList>

How can I then retrieve its selected value?

This is the code I'm trying to use:

        if (Request.Form.Count > 0)
            lblTest.Text = Convert.ToString(Context.Request.Form["countySelect"]);
        else
            lblTest.Text = "nada";

The result is blank.

If your DDL is inside a naming container, you'll need to use the UniqueID property of the control. Try Context.Request.Form[countySelect.UniqueID] . (I'm pretty sure UniqueID is the one you want but if it doesn't work, try ClientID). Also, you could hook up the debugger and take a look at everything in Request.Form to see what the contents are and perhaps that could help you.

Is this being processed on the same page as the DDL is on? If so you can just use countySelect.SelectedValue. Since you have Context.Request rather than just Request, I'm guessing it is not the same page though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM