I have an empty DropDownList in Asp.net, which is declared this way:
<asp:DropDownList ID="drpBranch" runat="server">
I add values to it in the Page_Load with Item.add:
drpBranch.Items.Add("Add New");
The thing is, that everytime I try to read the SelectedItem it returns 0.
Why is that?
I tried DataBind() before and after adding values, but it still didint work.
Thanks a lot 🙂
2
Answers
It is because you are not adding items with values, for example you are adding item with this : drpBranch.Items.Add("Add New"); // no value have been added with the item
You should do this :
drpBranch.Items.Insert(0, new ListItem("Select", "0")); // it will add item with a value.
Two things to keep in mind.
First up, page load fires EACH and every time a button click or any post-back on the page occurs.
So if you re-load the dropdown list each time on page load, then this occurs:
User makes a selection in drop down, then say hits a submit button.
Then:
Page load runs, re-loads dropdown list, and OVER writes the user’s selection, AND THEN your button code runs!
So, for any button click etc., keep in mind that the page load will fire each time, and fire first (before) the button code stub runs.
So, let’s take this markup:
And our page load event to "load up" some choices:
Note VERY close how we used the IsPostBack flag. That is used to run the code ONLY on the very first "real" page load. If we don’t use the test for IsPostBack = false (not IsPostBack), then when you click on the button, the page load ALWAYS runs, and with code to re-load dropdown list items, then the user selection will be lost (over written if our load up dropdownlist runs each time).
Note that a dropdown list, or a listbox has 2 values. One value is the "text" that the user sees, and you are also allowed a "hidden" value. This is common, since you might say select a hotel name and display the text, but the hidden value might be the Hotel database PK id for example.
So, the code can be this:
Note how there are potential 3 different values we can use from the drop down.
The Text display "text"
The hidden "value"
The index of the selection starting at 0
So, our button code can thus be this:
And the result now looks like this:
So, several things to keep in mind:
We can’t run code EACH time to re-load the dropdown list else we over write the user selection.
The page load event will trigger EACH time a button is clicked, and the page load event fires and runs BEFORE the button stub.
Thus page load code to setup controls, load data etc. MUST be placed in the IsPostBack = false code stub, else the page in general will not work correctly, since page load runs over and over during regular operation of that page with any post-backs.
I would "easy" state that the last 200 web pages I have created ALL have that IsPostBack = false stub in page load. In fact, you can’t really even build a working web page without such a code stub.
However, once you adopt and use the IsPostBack stub = false part in your code? Then that code is free to load up gridviews, setup controls on the page for first time use, and that code will ONLY run the first time on the first page load (after that, the code is ignored and not run each time a post-back of the page occurs).
The dropdown list has three possible values it can return, the Text, the hidden Value, and the index of the selection.