Reputation: 2155
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
Upvotes: 0
Views: 1508
Reputation: 2155
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
Upvotes: 0
Reputation: 415735
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:
Visible
to true for the one you care about.Upvotes: 3
Reputation: 846
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);
}
}
Upvotes: 0