Dynamic Control Postback Issue
-
Hi Im having a nightmare with a postback sequence so hopefully someone can give me ideas on how to solve this. Hopefully I will be able to explain my issue correctly. I have a Repeater on a page which contains a User Control in its Item Template The User Control has (among other things) a place holder control. Another user control is loaded dynamically with LoadControl (based on a database id) and added to the placeholder. So basically I end up with
Repeater
- UserControl (PageComponentController)
-- DynamicUserControl - UserControl (PageComponentController)
-- DynamicUserControl
etc
Repeater_ItemDataBound Snippet
CMS.Controls.PageComponentController pagecomponentcontroller = (CMS.Controls.PageComponentController)e.Item.FindControl("PageComponentController");
if (pagecomponentcontroller != null)
{
pagecomponentcontroller.PageComponent = pagecomponent;
pagecomponentcontroller.EditMode = _editmode;
pagecomponentcontroller.Zone = _zone;
pagecomponentcontroller.VirtualPage = _virtualpage;
}PageComponentController onLoad Snippet
_component = (CMS.Components.BaseComponent)LoadControl(_pagecomponent.Component.UserControlPath);
if (_component != null)
{
_component.PageComponent = _pagecomponent;
_component.EditMode = _editmode;
_component.VirtualPage = _virtualpage;
_component.Zone = _zone;
phComponent.Controls.Add((UserControl)_component);
}The UserControl (PageComponentController) contains two buttons "Edit" and "View" Which then sets a property inside DynamicUserControl This property is saved to the ViewState of the DynamicUserControl
public DAL.ComponentViews View
{
get
{
Object o = ViewState["COMPONENTVIEW"];
return (o == null) ? DAL.ComponentViews.View : (DAL.ComponentViews)o;
}
set
{
ViewState["COMPONENTVIEW"] = value;
}
}First problem is this viewstate is not remembered (always null) on postback so the control always displays its default state rather than the selected view. All the controls are populated again and show, but not the correct state. Is there anyway to save the state of dynamically loaded controls? Also, is it possible to tell which dynamic user control fired the postback? With a postback all the page_loads fire for all the controls on the page. The events wire
- UserControl (PageComponentController)
-
Hi Im having a nightmare with a postback sequence so hopefully someone can give me ideas on how to solve this. Hopefully I will be able to explain my issue correctly. I have a Repeater on a page which contains a User Control in its Item Template The User Control has (among other things) a place holder control. Another user control is loaded dynamically with LoadControl (based on a database id) and added to the placeholder. So basically I end up with
Repeater
- UserControl (PageComponentController)
-- DynamicUserControl - UserControl (PageComponentController)
-- DynamicUserControl
etc
Repeater_ItemDataBound Snippet
CMS.Controls.PageComponentController pagecomponentcontroller = (CMS.Controls.PageComponentController)e.Item.FindControl("PageComponentController");
if (pagecomponentcontroller != null)
{
pagecomponentcontroller.PageComponent = pagecomponent;
pagecomponentcontroller.EditMode = _editmode;
pagecomponentcontroller.Zone = _zone;
pagecomponentcontroller.VirtualPage = _virtualpage;
}PageComponentController onLoad Snippet
_component = (CMS.Components.BaseComponent)LoadControl(_pagecomponent.Component.UserControlPath);
if (_component != null)
{
_component.PageComponent = _pagecomponent;
_component.EditMode = _editmode;
_component.VirtualPage = _virtualpage;
_component.Zone = _zone;
phComponent.Controls.Add((UserControl)_component);
}The UserControl (PageComponentController) contains two buttons "Edit" and "View" Which then sets a property inside DynamicUserControl This property is saved to the ViewState of the DynamicUserControl
public DAL.ComponentViews View
{
get
{
Object o = ViewState["COMPONENTVIEW"];
return (o == null) ? DAL.ComponentViews.View : (DAL.ComponentViews)o;
}
set
{
ViewState["COMPONENTVIEW"] = value;
}
}First problem is this viewstate is not remembered (always null) on postback so the control always displays its default state rather than the selected view. All the controls are populated again and show, but not the correct state. Is there anyway to save the state of dynamically loaded controls? Also, is it possible to tell which dynamic user control fired the postback? With a postback all the page_loads fire for all the controls on the page. The events wire
Tyrone Watt wrote:
Is there anyway to save the state of dynamically loaded controls?
Sadly, your controls need to be created BEFORE page load, in order to participate in the reconstruction of the viewstate tree. Otherwise, the values are dropped. The alternative, is to store a hidden field in your page, and have javascript store the state of controls in that, because then you'll have something to set the state with, when you recreate the controls.
Tyrone Watt wrote:
Also, is it possible to tell which dynamic user control fired the postback?
Same issue, really. No click event, unless the control existed before page load.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
- UserControl (PageComponentController)
-
Tyrone Watt wrote:
Is there anyway to save the state of dynamically loaded controls?
Sadly, your controls need to be created BEFORE page load, in order to participate in the reconstruction of the viewstate tree. Otherwise, the values are dropped. The alternative, is to store a hidden field in your page, and have javascript store the state of controls in that, because then you'll have something to set the state with, when you recreate the controls.
Tyrone Watt wrote:
Also, is it possible to tell which dynamic user control fired the postback?
Same issue, really. No click event, unless the control existed before page load.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
Hi Thanks for that. I have no problem creating the controls in PageInit instead but either way that doesnt seem to be the case. The click event fires correctly after the page load, and the viewstate is set correctly in the "onclick" event, but it is not set in the page_load. So somewhere between the page_load and the onclick the viewstate is set back. Problem is I need to do things based on that viewstate before the click event or regardless of whether a click event occurred at all. With the second question, maybe i didnt phrase it correctly. What I need to know is which particular control fired the postback in the page_load. So if i have 6 user controls on a page and control 3 fired the postback. On the page_load for the page and all 6 controls is there a way to tell that it was control 3 that did the postback, or is a javascript hack with a hidden field the only way. As the event fires in the correct control, surely the must be some means of knowing which control fired the postback rather than just knowing that a postback occurred from one of the 6 controls. Thanks
-
Hi Thanks for that. I have no problem creating the controls in PageInit instead but either way that doesnt seem to be the case. The click event fires correctly after the page load, and the viewstate is set correctly in the "onclick" event, but it is not set in the page_load. So somewhere between the page_load and the onclick the viewstate is set back. Problem is I need to do things based on that viewstate before the click event or regardless of whether a click event occurred at all. With the second question, maybe i didnt phrase it correctly. What I need to know is which particular control fired the postback in the page_load. So if i have 6 user controls on a page and control 3 fired the postback. On the page_load for the page and all 6 controls is there a way to tell that it was control 3 that did the postback, or is a javascript hack with a hidden field the only way. As the event fires in the correct control, surely the must be some means of knowing which control fired the postback rather than just knowing that a postback occurred from one of the 6 controls. Thanks
1 - if you create the exact same control tree, viewstate should be restored 2 - yes, I got that. If you have viewstate, then the sender property of the event is passed the control that fired it, so you can access it that way. In the absence of viewstate, you don't get an event at all. As soon as an event fires, cast the sender to the right type and then identify it from there.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
1 - if you create the exact same control tree, viewstate should be restored 2 - yes, I got that. If you have viewstate, then the sender property of the event is passed the control that fired it, so you can access it that way. In the absence of viewstate, you don't get an event at all. As soon as an event fires, cast the sender to the right type and then identify it from there.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
1. Thats what I thought, but its not... And as you mention in 2
Christian Graus wrote:
In the absence of viewstate, you don't get an event at all.
Which puzzles me because the event fires, but the viewstate property is still null. With 2, the "sender" in the onload is always the control itself, so unfortunately that doesnt help. Its in the onload that i need to know which control fired the postback, not in the onclick event(where the sender is the control that fired the event). Thanks for you input so far, do you have any other suggestions i can try. Been beating my head against a wall for a while on this now and its starting to hurt... :sigh: