We help IT Professionals succeed at work.

table disappears

csetzkorn
csetzkorn asked
on
Medium Priority
296 Views
Last Modified: 2008-03-17
Hi,

I fill a Table from my code-behind file (C# ASP.NET 2.0) which is supposed to represent a form.

<asp:Table ID="input_table" runat="server" CellPadding=10 GridLines="Both" BackColor = white>
    </asp:Table>

The table is successfully created here:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // create table

            … invoke code to create the table

            Session["input_table"] = this.input_table;
}
else
{

}
}

The table is made persistent as shown above. The page also contains a button. In its OnClick method I would like to access the entries of the user (in the table cells). Unfortunatley, the table disappears  when the button is clicked. I am just wondering why this is and how I can prevent it? Any input would be very much appreciated. Many thanks in advance.

Chris
Comment
Watch Question

Commented:
You need to get the table back out of session state in your else clause (if it is a postback).

this.input_table=Session["input_table"]


Author

Commented:
sorry, I did this but forgot to add it to my post. I seem to be able to access the table in the OnClick method but it disappears.

C

Author

Commented:
Hi,

It still disappears. Any feedback would be very much appreciate. This is the 'correct code' I am using:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          // create table

          … invoke code to create the table

          Session["input_table"] = this.input_table;
}
else
{
this.input_table = (Table)Session["input_table"];
}
}

Commented:
You are creating the tabel in the if clause. Are you also adding it to the pages controls collection here. If so you need to do this in the else clause also. Please send your complete code.

Author

Commented:
The table already exists in the aspx file

<asp:Table ID="input_table" runat="server" CellPadding=10 GridLines="Both" BackColor = white>
</asp:Table>

In Page_Load I add objects (e.g. TextBox[es]) to the table cells. This works fine. The table is shown like a form with all these elements. When I click this button:

<asp:Button id="validate_entries" Runat="server" Font-Bold="True" Text="Validate Entries" OnClick="validateEntriesClick"></asp:Button>
   
I can still access all the objects in the table within the 'validateEntriesClick' method but the table becomes invisible. The question is why this happens? This:

this.input_table = (Table)Session["input_table"];

does not seem enough to make it reappear. Perhaps I do not fully understand the life cycle of the page but I doubt I have to recreate everything ...

Thanks,

C

Author

Commented:
If I am using:

protected void Page_Load(object sender, EventArgs e)
{
         // create table

          … invoke code to create the table
}

everything works fine (I even don't lose the data entered by the user???). However, this would be too inefficient wouldn't it as the table is created every time ...??!!

C
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you are adding the Table dynamically, all dynamic controls need to be recreated on post-back.

Bob

Author

Commented:
This might be a stupid question. Why can I not put the whole (dyn. created) object (table), which contains all the other objects, and put it in a session object and then (after the postback) do this:

Session["input_table"] = this.input_table;

as I have done before.

Everything works fine if I recreate the table each time after a postback. I am just wondering, why ASP.NET 2.0 is 'clever enough' to restore the values entered by a user (they are still their after a postback).

Thanks.

Christian
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
You can store anything serializable into the Session, but that doesn't mean that it is added to the form through that mechanism.  It needs to be rendered in the output HTML.

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
ok thanks. that makes sense.

could you possibly answer the second part of my last question?

Thanks.

C
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you are storing the Table in the Session, on post back are you recreating the Table, or getting it from the Session variable and rendering that?

Bob

Author

Commented:
no. I am just doing this:

protected void Page_Load(object sender, EventArgs e)
{
         // create table

          … invoke code to create the table
}

and it works as I wanted. It even persists the values entered by users (e.g. after a refresh). I am just wondering why this is? ASP.NET 2.0 seems to do this automatically which is nice.

Thanks,

Christian

Commented:
Yes asp.net does this automatically. It can even raise echanged events as it knows what the previous values was (from viewstate) and it knows what the new values is (from the post collection).

 

Author

Commented:
Ok thanks for your feedback. I accept Bob's comment (11/08/2006 04:54AM PST
) as it clarifies things.

Chris
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.