【发布时间】:2016-04-04 15:33:09
【问题描述】:
我遇到了显示学生姓名的 DropDownList 的问题。出于某种原因,在我单击一个用于给它们评分的按钮后,它会丢失项目。我想找到一种方法来保存这些物品。该按钮不会以任何方式过滤显示的学生。生成的 DropDownLists 也应该将 autopostback 设置为 true。学生姓名不会在后面的代码中检索或更改,因此我不确定为什么这些姓名会从此 DropDownList 中消失。欢迎任何提示/解决方案。更新:我已经附加了来自前端的代码以及来自 .cs 文件的代码,用于为学生发送标记的按钮。输入分数后返回模块,出现项目消失问题。
<asp:SqlDataSource
ID="SQLStudentList"
runat="server"
ConnectionString="<%$ ConnectionStrings:UniString %>"
SelectCommand="SELECT DISTINCT students_profile.user_id, (first_name + ' ' + last_name ) AS studentDetails FROM students_profile INNER JOIN classlist ON students_profile.user_id = classlist.user_id INNER JOIN class ON class.class_id = classlist.class_id INNER JOIN student_module_grade ON classlist.classlist_id = student_module_grade.classlist_id INNER JOIN student_module_repeat_grades ON student_module_grade.classlist_id = student_module_repeat_grades.classlist_id WHERE class.pathway_year_id = @idpathway AND student_module_grade.module_on_pathway_id =@modpwayid OR student_module_repeat_grades.module_on_pathway_id=@modpwayid">
<SelectParameters>
<asp:ControlParameter Name="idpathway" ControlID="degreeProgDropDown" Type="String"/>
<asp:ControlParameter ControlID="modDropDown" Name="modpwayid" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="StudentsList"
OnSelectedIndexChanged="StudentsList_SelectedIndexChanged"
runat="server"
width="420"
AutoPostBack="true"
EnableViewState="true"
DataSourceID="SQLStudentList"
DataTextField="studentDetails"
DataValueField="user_id">
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
////If there are no students the message below will be displayed
ListItem selectedItem = StudentsList.SelectedItem;
if (selectedItem != null && !String.IsNullOrEmpty(selectedItem.Text))
{
}
else
{
changedFlag.Visible = true;
changedFlag.Text = "There are currently no grades to change for any students for this module on this pathway";
changedFlag.ForeColor = System.Drawing.Color.Red;
EnterFinalMark.Visible = false;
finalMarkAssignment.Visible = false;
submitAssignmentMark.Visible = false;
repeatSubmitAssignmentMark.Visible = false;
}
if (!IsPostBack)
{
StudentsList.DataSource = SQLStudentList;
StudentsList.DataBind();
String userName = Session["UserLoggedOn"].ToString();
String conString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString;
SqlConnection myCon = new SqlConnection(conString);
myCon.Open();
String pathwaySelectionQuery = "SELECT pathway_years.id, pathway_years.pathway_year, pathway FROM pathways INNER JOIN pathway_years ON pathways.id = pathway_years.pathway_id";
SqlCommand pathwaySelectionQuerycmd = new SqlCommand(pathwaySelectionQuery, myCon);
SqlDataReader pwayReader = pathwaySelectionQuerycmd.ExecuteReader();
while (pwayReader.Read())
{
//Put pathway year id in this table instead
degreeProgDropDown.Items.Add(new ListItem(pwayReader["pathway_year"] + ": " + pwayReader["pathway"].ToString(), pwayReader["id"].ToString()));
}
myCon.Close();
}
}
protected void repeatSubmitAssignmentMark_Click(object sender, EventArgs e)
{
String connectionString = WebConfigurationManager.ConnectionStrings["UniString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
String repeatModgradeID = "SELECT repeat_module_grade_id from student_module_repeat_grades WHERE module_on_pathway_id = '" + modDropDown.SelectedValue + "'";
SqlCommand repeatModuleGradeIDCommand = new SqlCommand(repeatModgradeID, myConnection);
Int32 repeatModGradeIDResult = Convert.ToInt32(repeatModuleGradeIDCommand.ExecuteScalar().ToString());
String repeatFindUserID = "SELECT classlist_id from classlist WHERE user_id = '" + StudentsList.SelectedValue + "'";
SqlCommand repeatFindUserIDCommand = new SqlCommand(repeatFindUserID, myConnection);
Int32 repeatClasslistval = Convert.ToInt32(repeatFindUserIDCommand.ExecuteScalar().ToString());
String modOnPwayValue = modDropDown.SelectedValue;
String repeatGrade = finalMarkAssignment.Text;
Int32 repeatGradeval = Convert.ToInt32(repeatGrade);
//Grade is a pass if it is equal to or greater than 40- otherwise it is a fail
if (repeatGradeval >= 40)
{
//Pass assigned to the string which will be added to the table
String passOrFail = "Pass";
//Assigned to label
pOrF.Text = passOrFail;
}
else
{
//Fail assigned to the string which will be added to the table
String passOrFail = "Fail";
//Assigned to label
pOrF.Text = passOrFail;
}
if (repeatGradeval >= 0 && repeatGradeval <= 100)
{
changedVAL.Visible = false;
SqlCommand addAssignmentGradeCommand = new SqlCommand("UPDATE student_module_repeat_grades SET classlist_id=@repeatClasslistid,module_on_pathway_id=@modOnPwayValue,grade=@grade,result_code=@PF,changed=1 WHERE module_on_pathway_id = '" + modDropDown.SelectedValue + "'", myConnection);
addAssignmentGradeCommand.Parameters.AddWithValue(@"modOnPwayValue", modOnPwayValue);
addAssignmentGradeCommand.Parameters.AddWithValue(@"repeatClasslistid", repeatClasslistval);
addAssignmentGradeCommand.Parameters.AddWithValue(@"grade", repeatGradeval);
addAssignmentGradeCommand.Parameters.AddWithValue(@"PF", pOrF.Text);
addAssignmentGradeCommand.ExecuteNonQuery();
myConnection.Close();
success.Visible = true;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
success.ForeColor = System.Drawing.Color.Green;
repeatSubmitAssignmentMark.Visible = false;
}
else
{
changedVAL.Visible = true;
changedVAL.Text = "Please enter a grade between 0 and 100";
changedVAL.ForeColor = System.Drawing.Color.Red;
}
}
【问题讨论】:
-
只是我过去常犯的一个常见错误的猜测。确保只需要在 Page_Load 上发生一次的事情在 if(!IsPostback) { ... } 内。或者实际上,为什么不将代码移动到 Page_Load 事件上?我并不特别喜欢使用 UI 来设置查询的想法。相反,我执行这些查询并在我希望它发生时准确地绑定结果(在这种情况下,可能在 Page_Load 上)。
-
@spiros 尝试检查您填写下拉代码的位置
-
@Webruster DropDown 数据仅来自前端,并根据我拥有的其他两个 DropDowns 中的值进行更改
-
@Spiros 发布您的
.CS代码
标签: c# asp.net dropdown autopostback