2016-04-04 5 views
0

私はDropDownListに問題があります。何らかの理由で、ボタンをクリックすると項目が失われます。ボタンをクリックすると、項目が失われます。私はこれらのアイテムを保存する方法を見つけたいと思います。どのボタンでも、表示された生徒をフィルタすることはできません。結果のDropDownListsには、自動ポストバックが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で一度だけ発生する必要があるものがif(!IsPostback){...}の内側にあることを確認します。実際、Page_Loadイベントで発生するようにコードを移動させるのはなぜですか?私はUIを使用してクエリを設定するという考えを特に気にしません。むしろ、私はそれらのクエリを実行し、結果を正確にバインドします(この場合、おそらくPage_Loadに)。 –

+0

@spirosは、ドロップダウンコードを埋める場所を確認しようとします。 – Webruster

+0

@Webruster DropDownデータは、フロントエンドからのもので、他の2つのDropDownの値に応じて変化します。 – Spiros

答えて

0

私の最初は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 
} 
+0

こんにちは、私はStudentsList.DataSourceを置く= SQLStudentList; StudentsList.DataBind();ポストバックメソッドがドロップダウンリストに結果を表示しないかどうかをチェックします。私のフロントエンドでは、このDropDownListsは他の2つのDropDownListsからの選択された値に依存します。それらはデータで埋められます – Spiros

+0

if文内の 'DataBind()'呼び出し(およびデータソースの以前の設定)を実行している場合、コードは期待通りに動作するはずです。それ以外のコードはありますか? –

+0

私はコードの後ろにコードのページを掲載しました – Spiros