需要在多行TextBox中显示SQL中的数据

时间:2013-04-28 12:03:00

标签: c# asp.net sql

在将基于控制台的应用程序集成到基于Web的应用程序的过程中,我遇到了以下问题:

我需要在多行文本框中显示数据(根据要求),以便每个记录都显示在文本框中而不会覆盖前一个(它应该在下一行)。< / p>

对于Windows Forms,我使用以下代码:

        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

使用多行文字框

2 个答案:

答案 0 :(得分:1)

您可以使用Environment.NewLine

textBox2.Text = predicted_grade + Environment.NewLine;

http://msdn.microsoft.com/en-GB/library/system.environment.newline.aspx

答案 1 :(得分: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(),则可以为TextBox控件创建扩展方法AppendText

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);

您还可以使用扩展方法为您处理\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