3

I have a whole lot of controls to be created dynamically. Where is the best place to run the code for that?

I have been running the CreateControls function (to create all controls) at Page_Load.

Now the problem is, when I uncheck/uncheck one particular dynamic checkbox control (autopostback = true), the checkbox is always set to "true" because the CreateControls function runs again at Page_Load on postback.

If I put the CreateControls function within the (!IsPostBack) of Page_Load, when I click on the dynamic checkbox control, all controls disappear.

I have been looking at this for days, any ideas appreciated!

EDIT: The CreateControls function binds all the controls to a Panel.

viv_acious
  • 2,429
  • 9
  • 34
  • 55
  • Why are you adding those controls dynamically? persisting those is a PITA; try to reassess your logic and come up with some other way of solving your problem. – roman m Mar 22 '13 at 01:33
  • Click on the "Related" links to the right of your question, you might find help there. – roman m Mar 22 '13 at 01:35
  • 1
    Thanks for your help guys - I need to create those controls dynamically for a reason - there are 30 controls that needed to be created based data from a back-end SQL table. – viv_acious Mar 22 '13 at 01:38
  • what if you drop them all on the page, and toggle the visibility based on the date from the DB? – roman m Mar 22 '13 at 01:43

1 Answers1

4

Try calling your CreateControls method in Page_Init method instead of Page_Load... the Init event fires before the form values are bound to the controls, so your default values will be overwritten with the correct data.

Glen Hughes
  • 4,712
  • 2
  • 20
  • 25
  • I am still a bit confused regarding IsPostBack. Do I need to put the CreateControl within if(!IsPostBack)? – viv_acious Mar 22 '13 at 02:38
  • No, you need to bind on postbacks as well. – Glen Hughes Mar 22 '13 at 02:40
  • thanks. Another question - at the end of my CreateControl() I have codes to add all of the controls to a Panel (i.e. Panel1.Controls.Add(chkbox)) Now using PreInit, I get an error on these codes "Object reference not set to an instance of an object". Where do I put these codes now? Thanks! – viv_acious Mar 22 '13 at 02:44
  • Page_Init seems to work better... I've always used it, but saw the MSDN article and assumed PreInit was better. I'll update my answer. – Glen Hughes Mar 22 '13 at 02:54
  • Thanks... I've ran into another problem: so CreateControl() dynamically creates controls like Table tb = new Table(). Then I assign them an ID like e.g. tb.ID = "table" + j.ToString() based on a loop. Now that I've made the changes to run in Page_init, it is giving me errors "Multiple controls with the same ID "table0" where found. FindControl requires that controls have unique IDs. – viv_acious Mar 22 '13 at 03:20
  • Dont worry about my previous comment!! – viv_acious Mar 22 '13 at 03:21
  • 1
    Are you executing `CreateControls` twice now? – Igor Mar 22 '13 at 03:21
  • It's working now - I just forgot to take off my CreateControl from Page_Load. Thank you SOO much Glen!! I really appreciate your help! – viv_acious Mar 22 '13 at 03:22
  • Yes Igor, you are right, I made a mistake. CreateControl should only be in Page_init only! – viv_acious Mar 22 '13 at 03:22
  • Can I ask another related question here?: On every postback, the screen jumps back to the top. Normally with this problem, I create an ScriptManager/UpdatePanel in the .aspx page for each control. But since this is all done dynamically, is there a way around this? – viv_acious Mar 22 '13 at 03:27
  • An UpdatePanel should probably work in this scenario as well. Otherwise, you could try using the MaintainScrollPositionOnPostBack Page directive, but I think it's a little quirky at times. – Glen Hughes Mar 22 '13 at 03:31
  • I have something like 25+ dynamic controls within my Panel. So I put the around my that is the correct approach right? I keep getting some microsoftAjax.js/JSScript runtime error>:Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. – viv_acious Mar 22 '13 at 03:40