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 回答