locked
How to: Set a value in a asp:DropDownList ? RRS feed

  • Question

  • Hi,

    I want to populate a dropdownlist with values which i retrieve from the metadata service. Next, I want to select the first item of the dropdownbox.

    Looks easy, but for some reason I can't get this to work. Whatever I try, the dropdownlist-item with value="1" is always selected.

    Any help is much appreciated!

    Best regards, Leonard 

        // no initial value selected
        ddlOpleidingsniveau.Items.Insert(0, new ListItem("Please select", ""));
    
        // get gender data from CRM
        var opleidingsniveaus = CrmPropertyHelper.GetMetadata("crmrow_prestudy", "crmrow_level") as PicklistAttributeMetadata;
        if (opleidingsniveaus != null)
        {
          foreach (Option opleidingsniveau in opleidingsniveaus.Options)
          {
            ddlOpleidingsniveau.Items.Add(new ListItem(opleidingsniveau.Label.LocLabels[0].Label, opleidingsniveau.Value.Value.ToString()));
          }
        }
        ddlOpleidingsniveau.SelectedValue = "";
    
            //ddlOpleidingsniveau.SelectedIndex = (ddlOpleidingsniveau.Items.IndexOf(ddlOpleidingsniveau.Items.FindByText("Please select")));<br/>        //ddlOpleidingsniveau.Items.FindByText("Please select").Selected = true;<br/>
    
    Tuesday, November 16, 2010 2:14 PM

Answers

  • I did some research and found really weird things. Like I said, the dropdownlist-item with value="1" is always selected.

    In the settings of CRM, I changed the value of the picklist-item from 1 to 10. I then run the webpage again, and now the picklist-item "Please select" is selected. It seems thus that when there is a picklist-item with value 1, it is always selected in a dropdownlist.

    Next thing I tried was deploying and running the webpage (instead of running it in Visual Studio). Now, the first item of the dropdownlist was selected ("Please select"). So for some reason, the webpage works correctly if it is deployed, but works 'uncorrectly' if it runs in Visual Studio.

    I tried restarting Visual Studio, but the dropdownlist-item with value="1" was still selected. Also restarting the computer didn´t fix the problem.

    Maybe it´s a bug in Visual Studio (?). Anyway, the problem does not occur when I deploy the webpage, so it's not a showstopper. 

    • Marked as answer by Leonard_Str Wednesday, November 17, 2010 8:36 AM
    Tuesday, November 16, 2010 3:49 PM

All replies

  • try setting it to -1
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Check out the CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM Forum Guidance on how to Help Us Help You
    Tuesday, November 16, 2010 2:27 PM
    Moderator
  • Thanks for your quick reply.

    I changed the code to

    ddlOpleidingsniveau.SelectedValue = "-1";

    but still the dropdownlist-item with value="1" is always selected...

    Tuesday, November 16, 2010 2:39 PM
  • sorry I meant .SelectedIndex = -1;
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Check out the CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM Forum Guidance on how to Help Us Help You
    Tuesday, November 16, 2010 3:08 PM
    Moderator
  • I did some research and found really weird things. Like I said, the dropdownlist-item with value="1" is always selected.

    In the settings of CRM, I changed the value of the picklist-item from 1 to 10. I then run the webpage again, and now the picklist-item "Please select" is selected. It seems thus that when there is a picklist-item with value 1, it is always selected in a dropdownlist.

    Next thing I tried was deploying and running the webpage (instead of running it in Visual Studio). Now, the first item of the dropdownlist was selected ("Please select"). So for some reason, the webpage works correctly if it is deployed, but works 'uncorrectly' if it runs in Visual Studio.

    I tried restarting Visual Studio, but the dropdownlist-item with value="1" was still selected. Also restarting the computer didn´t fix the problem.

    Maybe it´s a bug in Visual Studio (?). Anyway, the problem does not occur when I deploy the webpage, so it's not a showstopper. 

    • Marked as answer by Leonard_Str Wednesday, November 17, 2010 8:36 AM
    Tuesday, November 16, 2010 3:49 PM
  • ddlOpleidingsniveau.SelectedIndex = -1;  

    also didn't work....

    Tuesday, November 16, 2010 3:52 PM
  • try

    ddlOpleidingsniveau.SelectedIndex = 0;  


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Check out the CRM 4 to CRM 2011 JavaScript Converter Tool
    CRM Forum Guidance on how to Help Us Help You
    Tuesday, November 16, 2010 3:58 PM
    Moderator
  • try this..

    // no initial value selected
    ddlOpleidingsniveau.Items.Insert(0, new ListItem("Please select", "-1")); 
    ddlOpleidingsniveau.SelectedValue = "-1";


    Jai Ho CRM http://mscrmkb.blogspot.com Skype - amol.gholap

    Mark as answer if a post has answered the question
    Tuesday, November 16, 2010 4:04 PM
  • ddlOpleidingsniveau.SelectedIndex = 0;  

    as well as

    // no initial value selected
    ddlOpleidingsniveau.Items.Insert(0, new ListItem("Please select", "-1")); 
    ddlOpleidingsniveau.SelectedValue = "-1";
    
    gave the same result: the dropdownlist-item with value="1" is selected, and not the dropdownlist-item with "Please select".
    Tuesday, November 16, 2010 4:51 PM