简体   繁体   中英

Why is my DropdownList value not being passed from my code-behind to my C# class in asp?

I have a dropdownlist that gets a list of semesters from the database. Once a semester is selected, a dropdownlist of courses for that semester appears. Once a course is selected, a dropdownlist of students for that course appears. Once a student is selected, a form of assignment score info appears. When I submit the assignment, I want to pass the value of the student DDL from the SaveScore_Click function to my Grades.cs class. For some reason, the value of enrollmentId(student for the course) always returns 0. The student DDL is populated correctly with all student ID's. Why is this value not passing to my Grades.calculate() function?

Code-Behind(AddScore.aspx.cs)

public partial class AddScore : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                PopulateSemesterList();
        }
private void PopulateSemesterList()
        {
            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("dbo.GetSemesters", conn);
            conn.Open();

            DDSemesters.DataSource = cmd.ExecuteReader();
            DDSemesters.DataTextField = "semesterName";
            DDSemesters.DataValueField = "semesterId";
            this.DataBind();

            conn.Close();
            conn.Dispose();

            DDSemesters.Items.Insert(0, new ListItem("Select Semester", "0"));
            DDSemesters.SelectedIndex = 0;
        }

        protected void DDSemesters_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DDSemesters = sender as DropDownList;

            int selectedSemester = Convert.ToInt32(DDSemesters.SelectedItem.Value);

            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("dbo.GetCourses", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@semesterId", selectedSemester));

            conn.Open();

            DDCourses.DataSource = cmd.ExecuteReader();
            DDCourses.DataTextField = "courseName";
            DDCourses.DataValueField = "courseId";
            this.DataBind();

            conn.Close();
            conn.Dispose();

            DDCourses.Items.Insert(0, new ListItem("Select Course", "0"));
            DDCourses.SelectedIndex = 0;
            DDCourses.Visible = true;
            CoursesLbl.Visible = true;
        }

        protected void DDCourses_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList DDCourses = sender as DropDownList;

            int selectedCourse = Convert.ToInt32(DDCourses.SelectedItem.Value);

            string connstring;
            connstring = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("dbo.CourseEnrollment", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@courseId", selectedCourse));

            conn.Open();

            DDStudents.DataSource = cmd.ExecuteReader();
            DDStudents.DataTextField = "fullName";
            DDStudents.DataValueField = "enrollmentId";
            this.DataBind();

            conn.Close();
            conn.Dispose();

            DDStudents.Items.Insert(0, new ListItem("Select Student", "0"));
            DDStudents.SelectedIndex = 0;
            DDStudents.Visible = true;
            StudentLbl.Visible = true;

        }

        protected void DDStudents_SelectedIndexChanged(object sender, EventArgs e)
        {
            assignmentInfoDiv.Visible = true;

        }

        protected void SaveScore_Click(object sender, EventArgs e)
        {
                Grades studentGrade = new Grades();
                studentGrade.enrollmentId = Convert.ToInt32(DDStudents.SelectedItem.Value);
                studentGrade.calculate();
                AssignmentError.Text = Convert.ToString(studentGrade.enrollmentId);               

        }

    }

AddScore.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddScore.aspx.cs" Inherits="midterm.AddScore" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Select Semester"></asp:Label><br />
        <asp:DropDownList ID="DDSemesters" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDSemesters_SelectedIndexChanged" DataTextField="semesterName" DataValueField="semesterId"></asp:DropDownList><br />

        <asp:Label ID="CoursesLbl" runat="server" Text="Select Course" Visible="false"></asp:Label><br />
        <asp:DropDownList visible="false" ID="DDCourses" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDCourses_SelectedIndexChanged" DataTextField="courseName" DataValueField="courseId"></asp:DropDownList><br />

        <asp:Label ID="StudentLbl" runat="server" Text="Select Student" Visible="false"></asp:Label><br />
        <asp:DropDownList visible="false" ID="DDStudents" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDStudents_SelectedIndexChanged" DataTextField="fullName" DataValueField="enrollmentId"></asp:DropDownList><br />

        <div id="assignmentInfoDiv" runat="server" visible="false" style="margin-top: 50px;">

            <asp:Label ID="studentName" runat="server" Text="Student Name" ></asp:Label>

            <asp:Label ID="AssignmentLbl" runat="server" Text="Assignment"></asp:Label><br />
            <asp:DropDownList ID="AssignmentList" runat="server">
                <asp:ListItem Text="Assignment 1"></asp:ListItem>
                <asp:ListItem Text="Assignment 2"></asp:ListItem>
                <asp:ListItem Text="Assignment 3"></asp:ListItem>
                <asp:ListItem Text="Assignment 4"></asp:ListItem>
                <asp:ListItem Text="Assignment 5"></asp:ListItem>
                <asp:ListItem Text="Assignment 6"></asp:ListItem>
                <asp:ListItem Text="Assignment 7"></asp:ListItem>
                <asp:ListItem Text="Assignment 8"></asp:ListItem>
                <asp:ListItem Text="Assignment 9"></asp:ListItem>
                <asp:ListItem Text="Assignment 10"></asp:ListItem>
                <asp:ListItem Text="Quiz 1"></asp:ListItem>
                <asp:ListItem Text="Quiz 2"></asp:ListItem>
                <asp:ListItem Text="Midterm Project"></asp:ListItem>
                <asp:ListItem Text="Final Project"></asp:ListItem>
            </asp:DropDownList>                
            <asp:Label ID="earnedLbl" runat="server" Text="Points Earned:"></asp:Label>
            <asp:TextBox ID="earnedTxt" runat="server"></asp:TextBox>

            <asp:Label ID="possibleLbl" runat="server" Text="Points Possible:"></asp:Label>
            <asp:TextBox ID="possibleTxt" runat="server"></asp:TextBox>

            <asp:Button ID="SaveScore" runat="server" Text="Save" OnClick="SaveScore_Click" />
            <asp:Label ID="AssignmentError" runat="server"></asp:Label>
        </div>
    </div>
    </form>
</body>
</html>

Grades.cs

public class Grades
    {
        public int pointsEarned;
        public int pointsPossible;
        public int totalEarned;
        public int totalPossible;
        public int percentage;
        public int courseId;
        public string enrollmentId;
        public string assignmentName;
        public string error;
        public string letterGrade;
        public double score;

        public void calculate()
        {
           return enrollmentId;

            }
        }

Hi I think you have missed Databind method for dropdown control.

 DDSemesters.DataSource = cmd.ExecuteReader();
            DDSemesters.DataTextField = "semesterName";
            DDSemesters.DataValueField = "semesterId";
            this.DataBind();

Here this.DataBind() method is from System.Web.UI.Page.DataBind() Instead you have to bind dropdown using DDSemesters.DataBind() for both dropdown list as below

 DDSemesters.DataSource = cmd.ExecuteReader();
            DDSemesters.DataTextField = "semesterName";
            DDSemesters.DataValueField = "semesterId";
            DDSemesters.DataBind(); 

i think your grade.cs should be like this

public class Grades
{
    public int pointsEarned;
    public int pointsPossible;
    public int totalEarned;
    public int totalPossible;
    public int percentage;
    public int courseId;
    public string strenrollmentId;
    public string assignmentName;
    public string error;
    public string letterGrade;
    public double score;
      public string enrollmentId;
       {
        get { return strenrollmentId; }
        set { strenrollmentId= value; }
       }
}

And button click should be

 protected void SaveScore_Click(object sender, EventArgs e)
    {
            Grades studentGrade = new Grades();
            studentGrade.enrollmentId = DDStudents.SelectedItem.Value;
            //studentGrade.calculate();
            AssignmentError.Text = studentGrade.enrollmentId;               

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM