Johnny Doe
Johnny Doe

Reputation: 193

.txt contents inside of table

I'm trying to display the contents of a text file in a table. I've got it to successfully read the text file, and display the .txt file contents in a div, but it throws an error when I try to display the contents in a table.

Am I doing something wrong?

Here's my code-behind:

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
         }
    }
}

Upvotes: 0

Views: 288

Answers (4)

Esen
Esen

Reputation: 973

This code should help you

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

Thanks,

Esen

Upvotes: 1

James Johnson
James Johnson

Reputation: 46047

You really shouldn't use a text file to load content like that. Your bound to run into issues, because sooner or later access to that file will be locked. You can negate this risk by loading the contents into Cache and loading it from there.

Following the advice above, you would modify the example below to retrieve the text from Cache, but for the sake of clarity, and to answer your initial question, this example loads it directly form the file:

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

Upvotes: 0

Rab
Rab

Reputation: 35572

there is no such property as Innerhtml for table control. you need to assign your text to a column in a row of your table.

you code should be like this

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

Now on the server side you can write

TableCell1.Text=somestring;
TableCell2.Text=anotherstring;

Upvotes: 2

huMpty duMpty
huMpty duMpty

Reputation: 14460

You can do something like this.

You can place a LiteralControl inside the td where you need your data to be populated. Then

while (viewer.Peek() > 0)
                {
                    string txtskip = viewer.ReadLine();
                    yourLiteral.text += txtskip; 
                }

Upvotes: 0

Related Questions