将.txt文件中的内容读取到段落标签<p> </p>

时间:2019-01-15 06:21:52

标签: c# html asp.net

我想从.txt文件中读取内容,并在客户端的

段落标签中显示该内容。我绝对不知道我该怎么做,我也没有使用任何功能。请给我建议。我看了看,但似乎一切都符合我的要求making a paragraph in html contain a text from a file

protected void Button1_Click(object sender, EventArgs e){
        String line;
        String filePath = "C:\\Users\\Administrator\\Test.txt";
        try
        {
            //Pass the file path and file name to the StreamReader constructor
            StreamReader sr = new StreamReader(filePath);

            //Read the first line of text
            line = sr.ReadLine();

            //Continue to read until you reach end of file
            while (line != null)
            {
                //write the lie to console window
                Console.WriteLine(line);
                //Read the next line
                line = sr.ReadLine();
            }

            //close the file
            sr.Close();
            Console.ReadLine();
        }
        catch (Exception e1)
        {
            Console.WriteLine("Exception: " + e1.Message);
        }
        finally
        {
            Console.WriteLine("Executing finally block.");
        }
    }

2 个答案:

答案 0 :(得分:1)

使用<p runat="server" id="para" /> 在后面的代码中,使用此ID para并按如下所示设置其文本

para.InnerHtml = "sample text to be set"

答案 1 :(得分:0)

您可以像这样在aspx页面上将p标记设置为runat =“ server”

data NonEmpty a = One a | Cons a (NonEmpty a)

然后,在阅读所有行之后,在您的代码后面(aspx.vb文件)中执行以下操作:

<p runat="server" id="paragraph"></p>
相关问题