How to let the first Row in DropDown list being blank?(using sproc)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ellen89
    New Member
    • Nov 2007
    • 14

    #1

    How to let the first Row in DropDown list being blank?(using sproc)

    Hi All,

    I have a dropdown list control, and its items are loaded from database during the page_load.

    say, the values in the db table are not allowed to be NULL.

    I want to have the first row in dropdown list being blank. It means the user has to click the dropdown arrow to see the items.

    Thank you for your help in advance.

    ellen89
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by ellen89
    Hi All,

    I have a dropdown list control, and its items are loaded from database during the page_load.

    say, the values in the db table are not allowed to be NULL.

    I want to have the first row in dropdown list being blank. It means the user has to click the dropdown arrow to see the items.

    Thank you for your help in advance.

    ellen89
    In the databind event for the dropdownlist, do:

    VB
    Code:
    CType(Sender, DropDownList).Items.Insert(0, "")
    C#
    Code:
    ((DropDownList)Sender).Items.Insert(0, "");
    You might want to verify the C#...I don't recall if my case selection is correct.

    Comment

    • ellen89
      New Member
      • Nov 2007
      • 14

      #3
      Thank you for your reply.

      I did following:

      DropDownList1.D ataBind();
      DropDownList1.I tems.Add(new ListItem("", ""));

      it inserted the Blank Line at the Bottom of my list. Then I tried

      DropDownList1.I tems.Add(new ListItem("", ""));
      DropDownList1.D ataBind();

      Now this time nothing happend.

      Wondering to where to insert DropDownList1.I tems.Add(new ListItem("", ""))?

      Thanks,
      ellen89

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by ellen89
        Thank you for your reply.

        I did following:

        DropDownList1.D ataBind();
        DropDownList1.I tems.Add(new ListItem("", ""));

        it inserted the Blank Line at the Bottom of my list. Then I tried

        DropDownList1.I tems.Add(new ListItem("", ""));
        DropDownList1.D ataBind();

        Now this time nothing happend.

        Wondering to where to insert DropDownList1.I tems.Add(new ListItem("", ""))?

        Thanks,
        ellen89
        That's because a). "Add" adds the new item at the end...and you want it inserted at the beginning and b). When you databind the control, it erases the previous contents, so having added the new entry, the control is then immediately cleared and populated with new data.
        Code:
        DropDownList1.DataBind()
        DropDownList1.Items.[b]Insert[/b](0, "Select...")

        Comment

        • kunal pawar
          Contributor
          • Oct 2007
          • 297

          #5
          Using Add method u can add listitem at the last position, but using insert method u can insert list item at any position. and another thing after add/insert not need to call bind method

          Comment

          • ellen89
            New Member
            • Nov 2007
            • 14

            #6
            Thank you so much for all your help.

            Yes, after changing Add to Insert, I had my expected result.

            Thank you again.

            ellen89

            Comment

            Working...