Reputation: 704
I have a combobox on a web form. The user can select a single value in the list. When the "Save" button is pressed, it has to send the post. Amongst the combobox are a few other controls, such as textboxes. When I try to read out the information that has been posted I can't seem to find/access the selected value of the combobox. I can, however, read out the values from the textboxes just fine.
Here's the line of code I'm using to read the information:
project.CustomerId = Convert.ToInt32(
Request.Form["ctl00$MainContent$uxCustomerComboBox$HiddenField"]);
Thank you in advance.
Edit 1: This is how the combobox is built up (keep in mind it's inside of a table):
<asp:ComboBox ID="uxCategoryComboBox"
runat="server"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend">
This is how I'm trying to read the actual value of the combobox:
uxCategoryComboBoxId.Value = uxCategoryComboBox.SelectedItem.Value;
Edit 2: This is how we tried to read the SelectedValue using an eventhandler, whilst debugging:
<asp: DropDownList ID="uxCategoryComboBox" runat="server" EnableViewState="true" OnSelectedIndexChanged="setIndex" AutoPostBack="true">
</asp: DropDownList>
This is the method setIndex
:
protected void setIndex(object sender, EventArgs e)
{
_project[0].CategoryId = Convert.ToInt32(uxCategoryComboBox.SelectedValue);
}
Upvotes: 1
Views: 2973
Reputation: 12966
You shouldn't need to use the automatically-generated control IDs (e.g. ctl00$MainContent$uxCustomerComboBox$HiddenField
) in the code-behind, or access the Request.Form
object. The correct way to obtain the value of a drop-down list control is through the SelectedValue
property. So I think you should just be able to do this:
project.CustomerId = Convert.ToInt32(this.uxCategoryComboBox.SelectedValue);
Edit:
I've just realised you're using a ComboBox
control, not the standard DropDownList
. ComboBox isn't a standard ASP.NET control (as far as I know), so it could be that having ViewState turned off is causing this problem.
Some kind of "state" is necessary for the combo-box to be correctly populated during postbacks. The standard ASP.NET controls use ControlState
, so that they will work to some degree when ViewState
is turned off. It could be that this ComboBox control you're using is totally reliant on ViewState, so I'd try turning that on, either for the page as a whole or just on this control.
Upvotes: 0
Reputation: 4698
If all of the controls are runat="server" with unique ID's, then you could just access it directly in code behind.
Upvotes: 1