2013-04-28 19 views
1

在過程中我的基於控制檯的應用程序集成到基於Web的應用程序,我碰到了下面的問題來顯示數據從SQL:需要在多行TextBox

我需要顯示數據在多行文本框(根據需要),使每個記錄顯示在文本框中而不覆蓋前一個(它應該在下一行)。

對於Windows窗體我用下面的代碼:

 if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 
       predicted_grade = reader["Domain"].ToString(); 
       priority = "Priority: " + i; 
       predicted_grade = priority +" --- "+predicted_grade + "\r\n"; 
       textBox2.AppendText(predicted_grade); 
       i++; 
      } 
     } 

但由於AppendText通過屬性不會在ASP.net網站運行,我不知道該怎麼做。 請指引我,我應該如何使數據顯示爲:

Code | Course Name | Predicted_Grade 
1 | Science | A+ 
2 | Maths  | B 
3 | History | C 

使用多行文本框

回答

1

你可以在ASP.NET頁面中執行AppendText功能,方法是修改您的

predicted_grade = priority +" --- "+predicted_grade + "\r\n"; 
textBox2.AppendText(predicted_grade); 

predicted_grade = priority +" --- "+predicted_grade + Environment.NewLine; 
textBox2.Text += predicted_grade; 

或者,如果你在項目中的多個頁面使用AppendText(),您可以創建一個擴展方法AppendText到TextBox控件:

public static class MyExtensions 
{ 
    public static void AppendText(this TextBox textBox, String text) 
    { 
     textBox.Text += text; 
    } 
} 

要使用它,只要致電:

predicted_grade = priority +" --- "+predicted_grade + Environment.NewLine; 
textBox2.AppendText(predicted_grade); 

您還可以使用擴展方法t Ø照顧\r\n的你:

public static void AppendLine(this TextBox textBox, String text) 
    { 
     textBox.Text += text + Environment.NewLine; 
    } 

要使用它,只要致電:

predicted_grade = priority +" --- "+predicted_grade; 
textBox2.AppendLine(predicted_grade); 

P/S:\r\n不會在ASP.Net的TextBox工作,所以你需要使用Darren提到的NewLine