We help IT Professionals succeed at work.

Table Control

ayha1999
ayha1999 asked
on
I populate an asp.net table control dynamically in a button click event but it disappears after page postback. how can I keep there after the postback? I have already placed a Table in in the page instead of creating at run time and adding to a place holder.

please help

ayha
Comment
Watch Question

Can you please paste the code, it will help us in understanding the issue.

Author

Commented:
   <asp:Table ID="Table1" runat="server">
    </asp:Table>

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tr As TableRow = New TableRow
        Dim j As Integer = 0
        Do While (j < 10)
            Dim tc As TableCell = New TableCell
            tc.Text = "date_" & j
            tr.Cells.Add(tc)
            j = (j + 1)
        Loop
        Table1.Rows.Add(tr)
    End Sub

When I click another button the table disappears.

You need to generate the table on every postback as the table state is not persisted during the postbacks.

In Button_Click save a flag in the viewstate. e.g.
ViewState["gen"]=="y";

And in the page_load check this flag, if its "y" then generate the table again else don't generate it.

Author

Commented:
How can regenerate it in the page load event? please give the code.
I can provide the code in c#.

Create a common function to generate the table and call the same when required. e.g.

protected void generateTable()
{
    Dim tr As TableRow = New TableRow
        Dim j As Integer = 0
        Do While (j < 10)
            Dim tc As TableCell = New TableCell
            tc.Text = "date_" & j
            tr.Cells.Add(tc)
            j = (j + 1)
        Loop
        Table1.Rows.Add(tr)
}

Now in the page_load:

protected void Page_Load(Object sender, EvetArgs e)
{
     if (Viewstate["gen"] !=null && Viewstate["gen"].ToString()=="y")
     {
          generateTable();
     }
}

Now in the button click generate the code
protected void btn_Click
{
   generateTable();
   Viewstate["gen"].ToString()=="y";
}

This approach will render table only when the botton is clicked and subsequent postbacks

Author

Commented:
Thanks