1

ジェネリックリストがループされ、入力可能なすべての空のテキストボックスに入力されるまで、aspxページの固定数のテキストボックスに入力する良い方法は何ですか?

現在、次のものがありますが、リストに回答が少ない場合は、テキストボックスがあり、outOfBoundExceptionが発生します。

else if(!IsPostBack) {
    Groups group = new Groups();
    List<Answers> AnswerList = new List<Answers>();
    //Get the group since the session isn't null
    group = group.getGroupbyId(int.Parse(Session["group_Id"].ToString()));
    //Get the list of answers of the group
    AnswerList = Answers.retrieveAnswersBygroupIdandPageNumber(group.Group_Id, pageNumberLab);

    if (AnswerList.Count != 0) {
        while (TextAnswerNumber < AnswerList.Count) {
            ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

            foreach (Control control in cph.Controls) {
                if (control is Panel) {
                    Panel panel = (Panel)control;
                    foreach (Control controlInPanel in panel.Controls) {

                    //If the control is a textbox
                    if (controlInPanel is TextBox && controlInPanel.ID.Contains("txtAnswer")) {
                        //And if the control ID containst txtAnswer                                    
                        //IMPORTANT: If next programmer adds another textbox for an answer textbox the ID must be in the form txtMember*".
                        TextBox answerTextBox = (TextBox)controlInPanel;
                        // Check if the textbox is empty

                        if (answerTextBox.Text == "") {
                            //If it is, fill it with the answer that group filled in when it previously answered this question.
                            answerTextBox.Text = AnswerList[TextAnswerNumber].Answer;
                            answerTextBox.ReadOnly = true;
                            TextAnswerNumber++;
                        }
                    }
                }
            }
        }
    }
}
            }
4

1 に答える 1

2

AnswerListでforeachを実行します

else if(!IsPostBack) { 
    Groups group = new Groups(); 
    List<Answers> AnswerList = new List<Answers>(); 
    //Get the group since the session isn't null 
    group = group.getGroupbyId(int.Parse(Session["group_Id"].ToString())); 
    //Get the list of answers of the group 
    AnswerList = Answers.retrieveAnswersBygroupIdandPageNumber(group.Group_Id, pageNumberLab); 

    if (AnswerList.Count != 0) { 
        foreach(Answer ans in AnswerList) { 
            ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1"); 

            foreach (Control control in cph.Controls) { 
                if (control is Panel) { 
                    Panel panel = (Panel)control; 
                    foreach (Control controlInPanel in panel.Controls) { 

                    //If the control is a textbox 
                    if (controlInPanel is TextBox && controlInPanel.ID.Contains("txtAnswer")) { 
                        //And if the control ID containst txtAnswer                                     
                        //IMPORTANT: If next programmer adds another textbox for an answer textbox the ID must be in the form txtMember*". 
                        TextBox answerTextBox = (TextBox)controlInPanel; 
                        // Check if the textbox is empty 

                        if (answerTextBox.Text == "") { 
                            //If it is, fill it with the answer that group filled in when it previously answered this question. 
                            answerTextBox.Text = ans.Answer; 
                            answerTextBox.ReadOnly = true; 
                        } 
                    } 
                } 
            } 
        } 
    } 
} 
            } 
于 2012-05-09T12:15:20.260 に答える