Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Very strange. Your code seems to be perfectly fine. I tried to reconstruct your page with the following code behind:</p> <pre><code>public partial class DropdownTests : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadAllCoursesToDDL(); } } private List&lt;Course&gt; GetDummyCourses() { return new List&lt;Course&gt;{ new Course{ CourseCode = "A12", CourseName = "Introduction to Programming"}, new Course{ CourseCode = "B112", CourseName = "Theory of Computing"} }; } private void LoadAllCoursesToDDL() { this.coursesDropDownList1.Items.Clear(); List&lt;Course&gt; items = GetDummyCourses(); this.coursesDropDownList1.DataSource = items; this.coursesDropDownList1.DataTextField = "CourseName"; this.coursesDropDownList1.DataValueField = "CourseCode"; this.coursesDropDownList1.DataBind(); } protected void btnBack_Click(object sender, EventArgs e) { //Server.Transfer("~/Teacher/TeacherControlPanel.aspx?username=" + username); } protected void btnSave_Click(object sender, EventArgs e) { string code = coursesDropDownList1.SelectedItem.Value; string code2 = coursesDropDownList1.SelectedValue; } } public class Course { public string CourseCode { get; set; } public string CourseName { get; set; } } </code></pre> <p>It just worked perfectly fine. In case, try to use <code>coursesDropdownList1.SelectedItem.Value</code> instead of "<code>SelectedValue</code>" directly. Shouldn't matter, but just to give it a try.</p> <p>As a try, I would also create a new page, copy the aspx code there and create the codebehind for it. In my opinion there must be something more which causes it to fail. Do you create and add some fields to the UI dynamically at run-time? Some custom server controls etc?? Your problem could be that you loose your ViewState.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload