views:

498

answers:

3

Hi,

I've got a strange problem concerning dynamically loaded controls in a asp.net application.

So there is a control where user have to select some items and/or do some text input (textboxes). The control depends on a single dropdown list element.

So user A chooses a certain value in this dropdownlist "controlselector" -> on of the many controls will be loaded. After that the user clicks on save and then it should save it to the database.

The problem is following that not every item is saved into the databse.

I create and recreate the control at every Page_Load, i've turned autopost back on the "controlselector" but the control is loading at the page_load event. When trying to save the elements are empty, but not every item :(

MyCustomControl:
FillElements(someParameter)
{
  //fill some lists, dropdowns, checkboxes or whatever with some values from db
}

Foo Save()
{
   //Save selected input(also some textboxes)
  //and return an object
  return foo;
}

Page:
Page_Load()
{
   PlaceHolder.Clear();

  //with Createpath the path to the control is created and loaded
   PlaceHolder.Controls.Add(LoadControl(CreatePath(Selector.SelectedValue)));

   //some methods are started to fill some lists in the control
   ((MyCustomControl)PlaceHolder.Controls[0]).FillElements(someParameter);
}

Save_Button_Click()
{
    var myFoo = ((MyCustomControl)PlaceHolder.Controls[0]).Save();
    myFoo.DoSomethingElse();
}

it seems that sometimes the page remembers values and sometimes not... ver strange everything

thanks

[EDIT] The problem i see that, there is 2 time a dynamic fill action. 1.) deciding which and then loading the custom control 2.) filling the custom control with the parameters

A: 

You need to check for "IsPostBack" if it is you dont want to recreate those controls... its killing the values etc that you have in them.

try changing your code to something like this.

Page_Load()
{
  if(IsPostBack == false){
   PlaceHolder.Clear();

  //with Createpath the path to the control is created and loaded
   PlaceHolder.Controls.Add(LoadControl(CreatePath(Selector.SelectedValue)));

   //some methods are started to fill some lists in the control
   ((MyCustomControl)PlaceHolder.Controls[0]).FillElements(someParameter);
  }
}
YetAnotherDeveloper
This is so werd.
thinkzig
why is this weird?
nWorx
+2  A: 

Page_Load is too late in the life cycle to create dynamic controls, because state is restored to controls before the load event. This means you need to create your control earlier, or ASP.Net won't see it when it comes time to restore state. Try creating them in the Init event instead. Or, even better, try one of these options:

  • Create one custom control type that adapts itself as needed and have a normal instance of the control on the page.
  • Place all controls on the page but only set Visible to true for the one you care about.
Joel Coehoorn
when i move the creation to the page_init or oninit event, than it seems that my dropdownlist, wich decides which custom control needs to be loaded, has not any value yet.. so control is getting loaded
nWorx
i've tried your second option also, to place the controls on the page and just set them visible or not... but the problem still remains.. i think it is because the controls need some parameter to fill their dropdownlists ((MyCustomControl)PlaceHolder.Controls[0]).FillElements(someParameter);
nWorx
A: 

hi,

thanks for your help, but the problem was on something totally different the items which were loaded dynamically into the dropdowns which where also loaded dynamically, had some "\n" special character, but not every item thats why not every item got lost just few

i don't know if i should/can mark this as answer, because the problem was on a other place

nWorx