skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

            <h3>Select a option</h3>
            <asp:DropDownList ID="DropDownList1" runat="server" Width="200px">
    
            </asp:DropDownList>
    
            <br />
            <br />
    
            <asp:Button ID="cmdMySubmit" runat="server" Text="Submit"
                OnClick="cmdMySubmit_Click"
             />
            <br />
            <br />
            <br />
            <asp:Label ID="lblText" runat="server" Text=""></asp:Label>
            <br />
            <asp:Label ID="lblValue" runat="server" Text=""></asp:Label>
            <br />
            <asp:Label ID="lblIndex" runat="server" Text=""></asp:Label>
    

    And our page load event to "load up" some choices:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        If Not IsPostBack Then
            ' real first page load code here
            ' this code ONLY runs on very first
            ' page load - NOT each time!!
    
            DropDownList1.Items.Add(New ListItem("Please select", "0"))
            For i = 1 To 10
    
                DropDownList1.Items.Add(New ListItem("Item " & i, i))
    
            Next
    
        End If
    

    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:

    Protected Sub cmdMySubmit_Click(sender As Object, e As EventArgs)
    
        lblText.Text = DropDownList1.SelectedItem.Text
        lblValue.Text = DropDownList1.SelectedItem.Value
        lblIndex.Text = DropDownList1.SelectedIndex
    
    End Sub
    

    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:

    Protected Sub cmdMySubmit_Click(sender As Object, e As EventArgs)
    
        lblText.Text = "Text value = " & DropDownList1.SelectedItem.Text
        lblValue.Text = "Hidden value = " & DropDownList1.SelectedItem.Value
        lblIndex.Text = "Index of selection = " & DropDownList1.SelectedIndex
    
    End Sub
    

    And the result now looks like this:

    enter image description here

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search