Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |
Showing posts with label SPFieldChoice to the DropDownList. Show all posts
Showing posts with label SPFieldChoice to the DropDownList. Show all posts

Thursday, November 29, 2012

SPFieldChoice to the dropdown list programatically:


  1. List Name: MyList
  2. Choice Field Name: MyChoiceField and the values are
    1. MyChoice1
    2. MyChoice2
    3.  MyChoice3
First example to set the SPChoice field default value as dropdown list default value.


          try
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
                    SPList liMyLists = web.Lists["MyList"];
                    SPFieldChoice fcTypes = (SPFieldChoice)       
                                                liMyLists.Fields["MyChoiceField"];
                    ddlChoiceField.DataSource = fcTypes.Choices;
                   //To set choice field default value as dropdown list default value.
                    ddlChoiceField.SelectedValue = fcTypes.DefaultValue; 
                    ddlChoiceField.DataBind();
            }
            catch (Exception ex)
            {
               //code
             }
OutPut: