2016-04-04 53 views
0

我在使用DropDownList顯示學生名稱時遇到問題。出於某種原因,我點擊一個按鈕後會丟失項目,這是用來給他們評分的。我想找到一種方法來保存這些項目。該按鈕不以任何方式過濾顯示的學生。得到的DropDownLists也應該將autopostback設置爲true。學生姓名在後面的代碼中沒有被檢索或修改,所以我不確定爲什麼名字從這個DropDownList中消失。任何提示/解決方案將受到歡迎。更新:我已附加了前端的代碼,並且還提供了用於爲學生髮送標記的按鈕的.cs文件中的代碼。進入一個分數並返回到模塊後,輸入的是物品消失問題。DropDownList上的消失項目

<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 [email protected] OR [email protected]"> 

    <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 [email protected],[email protected],[email protected],[email protected],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; 
    } 
} 
+0

只是一個可能的猜測:

通常你可以僅由Page_Load事件本身內執行檢查解決這個問題。確保在Page_Load上只需要發生一次的事情在if(!IsPostback){...}內部。或者實際上,您爲什麼不移動代碼來發生Page_Load事件?我並不特別喜歡使用UI設置查詢的想法。相反,我執行這些查詢並在我希望發生的時候將結果準確綁定(在這種情況下,可能在Page_Load上)。 –

+0

@spiros嘗試檢查您正在填充您的下拉代碼的位置 – Webruster

+0

@Webruster DropDown數據僅來自前端,並根據另外兩個DropDown中的值進行更改,我有 – Spiros

回答

0

我最初的想法是,你很可能目前還沒有檢查,如果一個PostBackPage_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 
} 
+0

您好,當我把StudentsList.DataSource = SQLStudentList; StudentsList.DataBind();在檢查回發方法我沒有在下拉列表中得到任何結果。在我的前端,這個DropDownLists依賴於來自其他兩個DropDownLists的選定值。它們被填充數據, – Spiros

+0

如果您正在執行if語句中的'DataBind()'調用(以及之前的數據源設置),則代碼應該按預期工作。除此之外,你還有其他的代碼嗎? –

+0

我已經將我的頁面加載到 – Spiros