4

I have a choice column in my SharePoint list called Njësia, I have created a dropdown in my solution and I want to retrieve selected value from dropdown list.

I have a label called Label1 and I want to represent the selected item in dropdown list.

I have the following code:

SPFieldChoice YearChoice = (SPFieldChoice)sampleList.Fields["Njësia"];
        for (int i = 0; i < YearChoice.Choices.Count; i++)
        {
            DropDownList4.Items.Add(YearChoice.Choices[i].ToString());
        }  
  string selectedvalyear = DropDownList4.SelectedValue;
  Label1.Text = selectedvalyear;

I also tried this line:

string selectedvalyear = DropDownList4.Text;

The result of Label1 is null or it displays only the first item in dropdown I have enable autopostback but nothing happens Please help me!

5
  • @AakashMorya I am using it but because the fact that dropdown gets items directly from SP list I am thinking it has no value Commented Jul 11, 2016 at 9:28
  • Just do one thing. Debug this code using worker process and find whether value in DropDownList4 is set or not? Commented Jul 11, 2016 at 9:32
  • Look @AakashMorya if I use your code it shows me only the first item of my dropdown and when I try to change it is not going to be changed Commented Jul 11, 2016 at 9:33
  • If you are selecting the value from dropdown in GUI then surely you will get value. See my updated answer Commented Jul 11, 2016 at 9:35
  • Maybe my guide "How to populate a list column" could help: afrait.com/blog/auto-populate-list-column-en
    – Patrick
    Commented Jul 11, 2016 at 9:46

3 Answers 3

3

To get a SharePoint Choice Field value

SPFieldChoice myChoicesfield = (SPFieldChoice)list.Fields["FieldName"];
        for (int i = 0; i < myChoicesfield.Choices.Count; i++)
          {
            DropDownListName.Items.Add(myChoicesfield.Choices[i].ToString());
          }

To get a SharePoint Multiple Choice Field value

SPFieldMultiChoiceValue itemValue = 
new SPFieldMultiChoiceValue(item["FieldName"].ToString());
foreach (string choice in itemValue)
{
// value is in choice
}

Ref: Get and Set a SharePoint Multiple Choice Field Value in C#

Regarding your code

normally, the above code retrieves only one item or null because the selected value only gets one value if the dropdown has items or null if it's empty.

So try to get the count of dropdown list items to make sure that you have already retrieved the data properly using the above code before you assign the selected value to the label, in Selected Index Changed Event don't forget to surround your code with

If(!Page.IsPostBack)
{
Label.text = dropdownlist.Text;
}

P.S: The issue may not be tied to retrieving values but rather to the scenarios in your code.

10
  • can't make your code to work Commented Jul 11, 2016 at 11:39
  • Why ?!! it's normal that your above code retrieves only one item or null because the selected value only get one value if the dropdown has items or null if it's empty ,so that try to get the count of dropdown list items to make sure my above code bound to the drop-down list or not ! before you assign the selected value to the label, might be the problem is not related retrieving the value rather than your code scenarios ! Commented Jul 11, 2016 at 11:52
  • I cannot use SPFieldMultiChoiceValue it provides me an error Commented Jul 12, 2016 at 14:03
  • Are you checked this >> SPFieldChoice myChoicesfield = (SPFieldChoice)list.Fields["FieldName"]; for (int i = 0; i < myChoicesfield.Choices.Count; i++) { DropDownListName.Items.Add(myChoicesfield.Choices[i].ToString()); } Commented Jul 12, 2016 at 14:05
  • Kindly,Read my answer again I provided you with one choice and multiple choice also choice field is also drop-down list but you only changed it's style view to show as drop-down list instead of choice but the data type is same Commented Jul 12, 2016 at 14:08
3

From where DropDown1 came? In your code you are adding items in DropDownList4 and you are trying to access the value from DropDown1.

Your code should be like:

string selectedvalyear = Convert.ToString(DropDownList4.SelectedItem);
Label1.Text = selectedvalyear;

If you want to change the value of Lable1 from dropdown every time when you change item, then you must create a SelectedIndexChanged Event of DropDown and write above code in that event.

Solution-2:

You should write your code in :

If(!Page.IsPostBack)
{
SPFieldChoice YearChoice = (SPFieldChoice)sampleList.Fields["Njësia"];
        for (int i = 0; i < YearChoice.Choices.Count; i++)
        {
            DropDownList4.Items.Add(YearChoice.Choices[i].ToString());
        }  
  string selectedvalyear = DropDownList4.SelectedValue;
  Label1.Text = selectedvalyear;
}
7
  • It was a mistake I updated the question but still not working Commented Jul 11, 2016 at 9:30
  • Just do one thing. Debug this code using worker process and find whether value in DropDownList4 is set or not? Commented Jul 11, 2016 at 9:32
  • I am retrieving the choices in my page_load and for each time that I try to chose another item in my dropdown list it selects only the first item nothing else! Commented Jul 11, 2016 at 9:38
  • Then you must iterate it and go with selectedIndex and retrieve the value using DropDownList4.Items[YourIndex].Value; Your code is written in such a way it will always give you first item only. What you want to display in Label1? Commented Jul 11, 2016 at 9:41
  • still not working :( Commented Jul 11, 2016 at 9:45
2

Make sure you write dropdown change event. Keep autopostback property to true.

In dropdown change event of your dropdown write below code.

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
                Label1.Text = DropDownList4.SelectedItem.Text;
        }

This will show display value of selected choice

You can also use:

  Label1.Text = DropDownList4.SelectedItem.Value;

This will show value (not display value) of selected choice

Your HTML block should look like:

<asp:DropDownList ID="DropDownList4" runat="server" Height="22px" AutoPostBack="true" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged">

7
  • Unfortunately only the first item is being selected and when i try to change the value, it does not change it keeps only the first value Commented Jul 11, 2016 at 9:51
  • can you show HTML block of you dropdown control
    – Gaurravs
    Commented Jul 11, 2016 at 10:00
  • <asp:DropDownList ID="DropDownList4" runat="server" Height="22px" AutoPostBack="true"> </asp:DropDownList> Commented Jul 11, 2016 at 10:00
  • dude, you haven't used used the solution I provided.
    – Gaurravs
    Commented Jul 11, 2016 at 10:01
  • ? I don`t understand what should I do Gaurravs? Commented Jul 11, 2016 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.