sams5817
sams5817

Reputation: 1037

Page postback caused generated HTML gone

I come across with dynamically add or remove HTML table row by JavaScript from the blog below:

http://viralpatel.net/blogs/2009/03/dynamically-add-remove-rows-in-html-table-using-javascript.html

My objective is to have a ASP.NET FileUpload control for user to upload images, then the image name will be display dynamically on table row, the table should be generated from the JavaScript above.

The problem is each time I add image, the postback will cause the HTML table gone, how can I store the HTML table in somewhere?

I do consider using StringBuild class to append HTML table from code behind, but there will be an issue to remove rows.

Any better workaround to achieve this? please give your comment.

Thank you in advanced.

Upvotes: 1

Views: 452

Answers (3)

Seany84
Seany84

Reputation: 5596

Here is a simple mock-up using a literal control.

ASPX

        <asp:Literal ID="lbl1" runat="server"></asp:Literal>
        <br />
        <br />
        <asp:Button ID=btnAdd runat=server Text="Simulate Upload Complete()" onclick="btnAdd_Click" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

CS

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        lbl1.Text = "<table>";
        lbl1.Text += "<tr>";
        lbl1.Text += "<td>Row1</td>";
        lbl1.Text += "</tr>";
        lbl1.Text += "<tr>";
        lbl1.Text += "<td>Row2</td>";
        lbl1.Text += "</tr>";
        lbl1.Text += "</table>";
    }
}

protected void btnAdd_Click(object sender, EventArgs e)
{
    lbl1.Text = lbl1.Text.Replace("</table>", string.Empty);
    lbl1.Text += string.Format("<tr><td>{0}</td></tr>", TextBox1.Text);
    lbl1.Text += "</table>";
    TextBox1.Text = string.Empty;
}

Upvotes: 1

KodeKreachor
KodeKreachor

Reputation: 8882

You need to make sure that the code that generates and inserts your dynamic controls runs after every page postback. As long as you do this ViewState will ensure the values entered into your dynamic controls persist after the postback.

Upvotes: 1

Eric
Eric

Reputation: 3334

The ASP.NET FileUpload control is not very flexible, and there are more up to date ways to deal with file uploading. There is a great jQuery file upload plugin that, if I'm understanding your question correctly, does exactly what you want: http://blueimp.github.com/jQuery-File-Upload/

Upvotes: 2

Related Questions