views:

43

answers:

1

When I go to parse through the table, all the stuff I created programmatically is missing...

I must be forgetting something so that it's losing all the stuff that I built programmatically (I am only getting the shell of the table).

Any ideas?

If I put the table in a Session object after I programmatically create it then it works except all the values the user enters will not be there obviously.

  protected void btnSave_Click(object sender, EventArgs e)
        {
           SaveMainGrid(tblCC);

           // SaveMainGrid((System.Web.UI.WebControls.Table)Session["tblMain"]);
        }

  private void SaveMainGrid(Control control)
        {
            foreach (Control ctrl in control.Controls)
            {
                if (ctrl is RadNumericTextBox)
                {
                    RadNumericTextBox t = ctrl as RadNumericTextBox;
                }
                else
                {
                    if (ctrl.Controls.Count > 0)
                    {
                        SaveMainGrid(ctrl);
                    }
                }
            }
        }
+2  A: 

This is likely another case of asp.net lifecycle. If you dynamically create controls, they aren't going to be available in the viewstate unless you recreate them. Even then, I don't know that the values would be persisted.

(I think they might. It's been a while since I fought this particular Web Forms quark.) Check this question out for more info.

Why are you creating the table dynamically? Can you use an asp:GridView?

Andy_Vulhop
@Andy I don't think this is right"If you dynamically create controls, they aren't going to be in the viewstate" I'm pretty sure you meant you need to recreate the controls on postback prior to the viewstate being loaded.
Conrad Frix
The grid is pivoted in a couple odd ways that just binding it to a gridview will be too difficult..
punkouter
I think im missing the fundamentals of ASP.NET. One I create a table and send that to the client.. and they type in some values and click 'save' .. since those values are not put in something like a asp:Textbox then it doesnt save it like I am used to...
punkouter
aha! I got it .. it was all cause of is postback... im so use to adding it on every page that I forgot that I need to regenerate the table EVERY TIME since its not part of viewstate. im so stupid! // if (!Page.IsPostBack) // { CreateTopGrid(); CreateMainGrid(); // }
punkouter
@Conrad Frix: I was typing fast right after lunch. As such, I got ahead of myself. Obvious mistake. Thanks for pointing it out.
Andy_Vulhop
@punkouter: It would be best if the method(s) that create the grid were in the `OnInit` event, like Oded suggested.
Andy_Vulhop
@Andy Nice +1. By the way the values are indeed posted back.
Conrad Frix
@punkouter: Watch out for certain types of controls with this. Ajax Extenders for instance can't be loaded prior to ViewState without view state errors being thrown.
Conrad Frix
Good article about ViewState http://msdn.microsoft.com/en-us/library/ms972976.aspx
Danil