我有一个下拉列表,可以从数据库中获取一个学期列表。选择一个学期后,将显示该学期的课程下拉列表。选择课程后,将显示该课程的学生下拉列表。选择学生后,将显示一种分配分数信息。当我提交作业时,我想将学生DDL的值从SaveScore_Click函数传递给我的Grades.cs类。出于某种原因,enrollmentId(课程的学生)的值始终返回0.学生DDL正确填充所有学生ID。为什么这个值没有传递给我的Grades.calculate()函数?
代码隐藏(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;
}
}
答案 0 :(得分:0)
您好我认为您已经错过了下拉控制的Databind方法。
DDSemesters.DataSource = cmd.ExecuteReader();
DDSemesters.DataTextField = "semesterName";
DDSemesters.DataValueField = "semesterId";
this.DataBind();
这里this.DataBind()方法来自System.Web.UI.Page.DataBind()而不是你必须使用DDSemesters.DataBind()为下拉列表绑定下拉列表,如下所示
DDSemesters.DataSource = cmd.ExecuteReader();
DDSemesters.DataTextField = "semesterName";
DDSemesters.DataValueField = "semesterId";
DDSemesters.DataBind();
答案 1 :(得分:0)
我认为你的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; }
}
}
按钮点击应为
protected void SaveScore_Click(object sender, EventArgs e)
{
Grades studentGrade = new Grades();
studentGrade.enrollmentId = DDStudents.SelectedItem.Value;
//studentGrade.calculate();
AssignmentError.Text = studentGrade.enrollmentId;
}