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;
}