简体   繁体   English

为什么我的DropdownList值没有从我的后台代码传递给ASP中的C#类?

[英]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. 提交作业时,我想将学生DDL的值从SaveScore_Click函数传递到我的Grades.cs类。 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. 出于某种原因,enrollmentId(课程的学生)的值始终返回0。用所有学生ID正确填充了学生DDL。 Why is this value not passing to my Grades.calculate() function? 为什么此值不传递给我的Grades.calculate()函数?

Code-Behind(AddScore.aspx.cs) 代码隐藏(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 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 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. 嗨,我认为您已经错过了用于下拉列表控件的Databind方法。

 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 这里this.DataBind()方法来自System.Web.UI.Page.DataBind()相反,您必须使用DDSemesters.DataBind()为两个下拉列表绑定下拉列表,如下所示

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

i think your grade.cs should be like this 我认为您的grade.cs应该是这样的

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;               

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从JavaScript函数获取值到我的ASP.NET/C#代码隐藏 - Getting a value from a JavaScript function to my ASP.NET / C# Code-Behind 如何从我的 aspx 页面将参数传递到 C# 代码隐藏方法? - How do I pass a parameter into a C# code-behind method from my aspx page? ASP:将文件路径完整复制到我的代码隐藏方法中 - ASP: Copying a file path in full into my code-behind method 为什么我的XAML ToggleButton对隐藏的代码不可见? - Why is my XAML ToggleButton invisible to the code-behind? 为什么我的XAML控件没有出现在代码隐藏中? - Why are my XAML controls not showing up in code-behind? ASP.NET C#将方法移动到类文件并从代码隐藏调用它 - ASP.NET C# Moving Method to Class File and Calling it from Code-behind 为什么我在代码隐藏类中使用泛型方法会导致动态编译异常? - Why is my use of a generic method in a code-behind class resulting in a dynamic compilation exception? ASP.NET web 形式:显示值来自 c# 代码隐藏 - ASP.NET web form: display value coming from c# code-behind C#ASP.NET如何从Code-Behind插入Eval值重复器 - C# ASP.NET How to insert Eval value Repeater from Code-Behind 为什么asp:DropDownList不响应后台代码 - Why asp:DropDownList does not respond to code-behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM