DropDownList上消失的项目

时间:2016-04-04 15:33:10

标签: c# asp.net dropdown autopostback

我遇到了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;
    }
}

1 个答案:

答案 0 :(得分:0)

我最初认为您可能目前没有检查您的Page的PostBack事件中是否发生了Page_Load,这会导致您的数据每次都被反弹。

您通常只需在Page_Load事件本身内执行检查即可解决此问题:

protected void Page_Load(object sender, EventArgs e)
{
     // If it is an initial load
     if(!IsPostBack)
     {
         // Then perform your one-time data binding here
         StudentsList.DataSource = SQLStudentList;
         StudentsList.DataBind();
     }
     // Business as usual
}