ASP.NET - new user control added to control collection get unexpectedly a value -
I have created a user control container (actually a panel) that dynamically add and remove user control objects (like rows ). Here's the problem: When the new user is added to control the control archive, then the previous control gets unexpectedly a value - only on the first post-back.
Description in detail:
- In the initial reaction I fill a user control object.
- Then I click on the "Add" button, which adds new user control items to the panel, and this new control gets the value from the previous one (all the value to be more confusing new controls Do not see, only disabled text box controls and dropdown list). Why?
Then my implementation was with this problem:
UcSomeControlItem uci = LoadControl ("UcSomeControlItem .ascx") as UcSomeControlItem; Uci.DataSource = New SomeObject; Uci.DataBind (); Uci.ID = IdPrefix + ControlID; Ph1.Controls.Add (uci); // Finally
When I change the panel control collection sequence to add the problem, the UI gets the value on the panel. .
Like:
UCE = Load Control ("UCSOMControlOut.Iscx") as UCSOMICOICIOTMITMIICLODOdControl ITEM; Ph1.Controls.Add (uci); // First Adding .. uci.DataSource = newObject; Uci.DataBind (); Uci.ID = IdPrefix + ControlID;
So I would like to know what is happening? What value does the newly added control define?
As soon as you add control to the container when the container is already on the page, all the life cycles Events (Init, LoadViewState, Load, etc ...) are removed sequentially to the newly added control, until it does not catch until the life cycle of parental control is currently present.
Because you set the control's ID and then add it to the page, it has the same ID, as all the catch-up events are removed. So when LoadPostbackData is removed for control (which is seen after the fastest loading), ASP.Net assumes that it is already the same kind of control as the match of the id, and the values in the previous values Does the income to load.
When you change it, you add after to the page controls, then all lifecycle events are removed when the control has an auto-generated ID , Hence postback processor
Hope this is clear.
Comments
Post a Comment