I have written a simple asp.net c# code for getting the value of dropdownlist. But even after selecting some values I am always getting value as "--Select Vendor--"
Here is the code.
<asp:DropDownList ID="ddlVendorUploadData" runat="server" CssClass="form-control">
</asp:DropDownList>
<label>Vendor Name</label>
And code behind.
protected void btnUploadData_Click(object sender, EventArgs e)
{
try
{
string strUserRole = ddlVendorUploadData.SelectedItem.Value;
}
}
And button click code is
<asp:Button ID="btnUploadData" runat="server" class="btn btn-danger button" Text="Submit"
OnClick="btnUploadData_Click" OnClientClick="return ValidateDropdownlist();" />
2
Answers
This would be easier to answer if we could see the code that generates the dropdown data. But, as far as I can see, there could be three possible issues:
SelectItem.Value
andSelectedItem.Text
, considering that a dropdown item has aText=""
and aValue=""
property?DataValueField
property of the dropdown list to"Value"
(i.e.,.DataValueField = "Value";
)—again assuming that it’s theValue
property data that you want.ViewState
that is not setup properly. It can cause update problems if the viewstate is false.Hope any of these suggestions help solve you problem.
You don’t show the code to fill the drop list. But, MOST often this occurs due to you re-loading the drop down list.
Always, but ALWAYS put your FIRST forms load code in the IsPostBack = false.
Any post back, any button click will ALWAYS run the page load event again, and does so EACH time.
I really wish that a separate event called FirstPageLoad existed.
So, assuming this markup:
Note in above, we have TWO values. The first column is the hidden pk "ID" of the hotels table. And then we have the "display" value of HotelName.
So, MAKE SURE that you ONLY load up the data source for above ONE time. If you reload each time, then even on button click, the page load event fires, and your code re-loads the drop down, and you LOSE your selection when you re-load.
So, the code on page load should look like this:
Note the VERY important test/check for
IsPostBack
. Since page load runs on EVERY post-back (any button click for example), then we need a REAL first page load in which we load up controls, grids and do our REAL page load work and setup.So, use the
IsPostBack
test.The MyRst is just a handy helper routine that returns a data table – I use it over and over throughout my application.
That code is this:
And thus when we run, we get this:
So, after select, then we have this for the test button code:
I like to use SelectedItem ALWAYS , and then get the Text value (display) or the hidden Value property (ID). Note that if your drop does NOT have two values? Then I suggest setting DataValue and DataText both to the one column name.
Output:
Note HOW the text property actually returns the "value" setting. You can also set the drop with
and it will jump to, and display the text value
So, OFTEN we need to select a part or say hotel as per above, but we want to get/have/use/work with the PK or hidden column.
So, keep in mind the 2 column ability of the drop down.
And keep in mind the page load event – you need to place your combo load up code in the
IsPostBack = false
stub else you re-load the drop down EACH time on EACH post back (such as each button click).And as noted, to avoid confusing, I always use the
SelectedItem.Text
, orSelectedItem.Value
, and that reduces a boatload of confusion here.