2012-05-09 17 views
1

aspxページの固定数のテキストボックスを入力するには、一般リストがループされ、それが埋めることができる空のテキストボックス?一般リストがループされるまで、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++; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
      } 
+1

上のforeachを実行します。コントロールisPanel) 'チェック。 –

+0

あなたはなぜあなたがコードに魅了されていないかを詳しく説明できますか?私はそれがすぐに問題に関連していないが、それが明確になるかもしれないことを知っている。そして、それはうまくいく。私はそれをもう一度試したと思っていましたが、そうではないようです。編集:今覚えています。あなたが入力したものを試しました。 –

答えて

2

(場合に `(TextAnswerNumber

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; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
      } 
+0

私はかなり肯定的でした。私はそれを試みましたが、うまくいきませんでした。しかし、ドレイクはすでに私に解決策を提供して以来、私は今、それがうまくいかないかどうかをチェックするためにこれを試しません。とにかく感謝します。 –

+0

@reaper_unique私は自分のクリスの答えが好きです。試してみる時間があることを願っています。 –

+0

しかし、これがより良い解決策である理由、または自分が提供した解決策よりもこれを好む理由を教えてください。 また、テキストボックスにあるデータを自分のデータベースに書き込む必要があるかどうかを判断するために、「カウンター」TextAnswerNumberを使用してコードをさらに絞ります。答えを書き直すのは意味がありません。別の問題が生じます。つまり、データベースに二重のエントリを作成すると、ユーザーがこのページに再度アクセスしたときに再びテキストボックスを埋めるときに問題が発生します。 –

関連する問題