Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How do you have a DropDownList populated without default selected item?

How do you have a DropDownList populated without default selected item?

Scheduled Pinned Locked Moved ASP.NET
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11369001
    wrote on last edited by
    #1

    I have a RequestorDropDownList which is populated with a list of names from a GetRequestorInfoModel. I need help to make a change so that when this list is populated, the first name is not automatically selected until a user selects it. How do I do this based on my code below? ASP code for the dropdownlist

    Code behind that is populating the dropdownlist

    #region Requestor dropdownlist
    public async Task> GetRequestorEmail()
    {
    try
    {
    var requestors = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
    //Show first name as selected
    return requestors.OrderBy(x => x.DisplayName);
    }
    catch (Exception ex)
    {
    string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "PopulateRequestorComboBox()", ex.Message);
    Response.Write("alert(" + HttpUtility.JavaScriptStringEncode(errorMsg, true) + ")");
    return Enumerable.Empty();
    }
    }
    #endregion

    Here is code for RequestorDropDownList_SelectedIndexChanged

    protected void RequestorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (RequestorDropDownList.SelectedItem ==null)
    {
    ListItem requestorItem = new ListItem();
    requestorItem = (ListItem)RequestorDropDownList.SelectedItem;

        if (requestorItem.Text.Length < 0)
        {
           string selectRequestor = "Please select a Requestor.";
           ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + selectRequestor + "');", true);
         }
         this.EmailButton.Enabled = false;
         RequestorDropDownList.SelectedValue = RequestorDropDownList.SelectedItem.Value;
     }
    

    }

    Z 1 Reply Last reply
    0
    • U User 11369001

      I have a RequestorDropDownList which is populated with a list of names from a GetRequestorInfoModel. I need help to make a change so that when this list is populated, the first name is not automatically selected until a user selects it. How do I do this based on my code below? ASP code for the dropdownlist

      Code behind that is populating the dropdownlist

      #region Requestor dropdownlist
      public async Task> GetRequestorEmail()
      {
      try
      {
      var requestors = await FTACaseReseting.Controllers.RequestorInfoController.GetAllRequestorInfoes();
      //Show first name as selected
      return requestors.OrderBy(x => x.DisplayName);
      }
      catch (Exception ex)
      {
      string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "PopulateRequestorComboBox()", ex.Message);
      Response.Write("alert(" + HttpUtility.JavaScriptStringEncode(errorMsg, true) + ")");
      return Enumerable.Empty();
      }
      }
      #endregion

      Here is code for RequestorDropDownList_SelectedIndexChanged

      protected void RequestorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
      {
      if (RequestorDropDownList.SelectedItem ==null)
      {
      ListItem requestorItem = new ListItem();
      requestorItem = (ListItem)RequestorDropDownList.SelectedItem;

          if (requestorItem.Text.Length < 0)
          {
             string selectRequestor = "Please select a Requestor.";
             ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + selectRequestor + "');", true);
           }
           this.EmailButton.Enabled = false;
           RequestorDropDownList.SelectedValue = RequestorDropDownList.SelectedItem.Value;
       }
      

      }

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      You can add an empty item, such as one that has text "Please select..." or something like that.

      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

      U 1 Reply Last reply
      0
      • Z ZurdoDev

        You can add an empty item, such as one that has text "Please select..." or something like that.

        Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

        U Offline
        U Offline
        User 11369001
        wrote on last edited by
        #3

        I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.

        Z M 2 Replies Last reply
        0
        • U User 11369001

          I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          Member 11403304 wrote:

          but I do not know how to add that text.

          Whatever code is creating the list just have it add in the blank one.

          Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

          1 Reply Last reply
          0
          • U User 11369001

            I actually have tried to add <--Please Select a Requestor--> but I do not know how to add that text. Do I add that text message in ASP code for the DropDownList or code behind that is populating dropdownlist? I really do not know how to add it but I want to add it with your help. When I debug, requestorItem = (ListItem)RequestorDropDwonList.SelectedItem is showing first name on the list. I want it to show null or nothing.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            You insert a new object into the first position in the list populating the drop down. You now know what the content of that item is so when you process the selection you check for the inserted item and deal with that.

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups