C# 表中的.txt内容

C# 表中的.txt内容,c#,asp.net,.net,C#,Asp.net,.net,我试图在表格中显示文本文件的内容。我已经成功地读取了文本文件,并在div中显示了.txt文件内容,但是当我试图在表中显示内容时,它会抛出一个错误 我做错什么了吗 下面是我的代码: protected void btnUpld_Click(object sender, EventArgs e) { Stream theStream = file1.PostedFile.InputStream; //file1 is an asp:FileUpload on .aspx

我试图在表格中显示文本文件的内容。我已经成功地读取了文本文件,并在div中显示了
.txt
文件内容,但是当我试图在表中显示内容时,它会抛出一个错误

我做错什么了吗

下面是我的代码:

protected void btnUpld_Click(object sender, EventArgs e)
{
    Stream theStream = file1.PostedFile.InputStream; 
    //file1 is an asp:FileUpload on .aspx

    using (StreamReader viewer = new 
           StreamReader((Stream)file1.PostedFile.InputStream))
    {
        while (viewer.Peek() > 0)
        {
            string txtskip = viewer.ReadLine();
            Table1.InnerHtml += txtskip; 
            //Table1 is the table i'm wanting to put the results in on my .aspx
         }
    }
}

你可以这样做

您可以在需要填充数据的
td
中放置一个。 然后


你可以这样做

您可以在需要填充数据的
td
中放置一个。 然后


表控件没有
Innerhtml
这样的属性。您需要将文本指定给表格行中的一列

你的代码应该是这样的

<asp:Table ID="Table2" runat="server">
    <asp:TableRow ID="TableRow1" runat="server">
        <asp:TableCell ID="TableCell1" runat="server">
        This is Cell 1
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        This is Cell 2
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

表控件没有
Innerhtml
这样的属性。您需要将文本指定给表格行中的一列

你的代码应该是这样的

<asp:Table ID="Table2" runat="server">
    <asp:TableRow ID="TableRow1" runat="server">
        <asp:TableCell ID="TableCell1" runat="server">
        This is Cell 1
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        This is Cell 2
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

你真的不应该使用文本文件来加载这样的内容。您的绑定将遇到问题,因为对该文件的访问迟早会被锁定。您可以通过将内容加载到
缓存
并从那里加载来消除这种风险

按照上面的建议,您可以修改下面的示例以从
缓存中检索文本,但为了清晰起见,并回答您的初始问题,此示例直接将其加载到文件中:

using (var reader = new StreamReader("bla.txt"))
{
    //use the object initializer to prepare a new literal control
    var litCtrl = new Literal 
    { 
        Mode = LiteralMode.PassThrough, 
        Text = string.Format("<table>{0}</table>", reader.ReadToEnd()))
    }

    //use a literal control to display the table on the page
    pnlContainer.Controls.Add(litCtrl);
}
使用(var reader=newstreamreader(“bla.txt”))
{
//使用对象初始值设定项准备新的文字控件
var litCtrl=new Literal
{ 
Mode=LiteralMode.PassThrough,
Text=string.Format(“{0}”,reader.ReadToEnd()))
}
//使用文字控件在页面上显示表格
pnlContainer.Controls.Add(litCtrl);
}

您真的不应该使用文本文件来加载这样的内容。您的绑定将遇到问题,因为对该文件的访问迟早会被锁定。您可以通过将内容加载到
缓存
并从那里加载来消除这种风险

按照上面的建议,您可以修改下面的示例以从
缓存中检索文本,但为了清晰起见,并回答您的初始问题,此示例直接将其加载到文件中:

using (var reader = new StreamReader("bla.txt"))
{
    //use the object initializer to prepare a new literal control
    var litCtrl = new Literal 
    { 
        Mode = LiteralMode.PassThrough, 
        Text = string.Format("<table>{0}</table>", reader.ReadToEnd()))
    }

    //use a literal control to display the table on the page
    pnlContainer.Controls.Add(litCtrl);
}
使用(var reader=newstreamreader(“bla.txt”))
{
//使用对象初始值设定项准备新的文字控件
var litCtrl=new Literal
{ 
Mode=LiteralMode.PassThrough,
Text=string.Format(“{0}”,reader.ReadToEnd()))
}
//使用文字控件在页面上显示表格
pnlContainer.Controls.Add(litCtrl);
}

此代码应该对您有所帮助

 protected void btnRender_Click(object sender, EventArgs e)
{
    using (StreamReader viewer = new StreamReader((Stream)file1.PostedFile.InputStream))
    {
        TableRow tr;
        TableCell tc;
        while (viewer.Peek() > 0)
        {
            string txtskip = viewer.ReadLine();
            //   Table1.InnerHtml += txtskip; //Table1 is the table i'm wanting to put the results in on my .aspx
            tr = new TableRow();
            tc = new TableCell();
            tr.Cells.Add(tc);
            Table1.Rows.Add(tr);
            tc.Text = txtskip;
        }
    }
}
谢谢


Esen

此代码应该对您有所帮助

 protected void btnRender_Click(object sender, EventArgs e)
{
    using (StreamReader viewer = new StreamReader((Stream)file1.PostedFile.InputStream))
    {
        TableRow tr;
        TableCell tc;
        while (viewer.Peek() > 0)
        {
            string txtskip = viewer.ReadLine();
            //   Table1.InnerHtml += txtskip; //Table1 is the table i'm wanting to put the results in on my .aspx
            tr = new TableRow();
            tc = new TableCell();
            tr.Cells.Add(tc);
            Table1.Rows.Add(tr);
            tc.Text = txtskip;
        }
    }
}
谢谢


Esen

您遇到了什么错误。您可能希望向表中添加行和单元格,并在单元格(单元格innerhtml)中写入内容,而不是表
“System.Web.UI.WebControls.table”不包含“innerhtml”的定义,并且找不到接受“System.Web.UI.WebControls.table”类型的第一个参数的扩展方法“innerhtml”(是否缺少using指令或程序集引用?。
。它指向
Table1.InnerHtml+=txtskip;
line出现了什么错误。您可能希望向表中添加行和单元格,并将内容写入单元格(单元格InnerHtml)相反,table
“System.Web.UI.WebControls.table”不包含“InnerHtml”的定义,并且找不到接受“System.Web.UI.WebControls.table”类型的第一个参数的扩展方法“InnerHtml”(是否缺少using指令或程序集引用?).
。它指向
Table1.InnerHtml+=txtskip;