Sunday, March 11, 2012

NESTED CONTROLS BUG

Well, I have solved my issue.

Not sure if this is actually a bug or it is something intended that I simply haven't studied on yet.

But, to solve the issue, I simply wrote a property called CurrentControl. Every time I load a control into the placeholder, I save the name of the control (using the set method of CurrentControl) into ViewState. Each time the parent control is requested, I check for postback and either load my default control, or in the case of a postback, the control that is saved in ViewState (via the get method of CurrentControl).

This is INSANELY simple and sort of erks me that the answer was more obvious.

ANYWAY! Hope this helps someone out.


i am having a similar issue...
EnableViewState="true" doesnt seem to be doing anything when used within a nested control (?) i am losing all of my state information every time i postback with different UpdatePanels


This is what I did...

the main control (navigation.ascx) contains linkbuttons and a placeholder (menuplaceholder)
The linkbuttons have a commandargument of whatever the name of the control should be loaded through that link (i.e. CommandArgument="contacts" OnCommand="LoadNewControl")
In the codebehind, I have my LoadNewControl sub which simple loads the control and adds it to the placeholder.

This worked fine on it's own. The problem I was having was that if the control (contacts.ascx, for example) did anything (i.e. used a paged datasource, or had linkbuttons), I would loose the control when I clicked anything.

So, I added

Public Property CurrentControl()As String Get Return ViewState("CurrentControl")End Get Set(ByVal valueAs String) ViewState("CurrentControl") = valueEnd SetEnd Property

to my codebehind...
I use that in Page_Load by checking IsPostBack
If it IS, rather than loading my default control, I load CurrentControl (which pulls the control name from ViewState).

I also found that when loading controls dynamically, issuing an ID helps. For example, when I load a control (Dim ctrl = LoadControl(...)), the control would work, but would require a link to be clicked twice before it did anything. When I set ctrl.ID = "whatever", this issue was resolved.

It should also be noted that this issue only happened when the control was nested. The nav control worked like a charm, even when I added UpdatePanels to it. It was only the control loaded into the placeholder of another control that seemed to have this problem.

So, the issue is that the viewstate is not being saved for nested controls.


sorry trying to think in terms of C# (:()

so adding something like this to the control's class would be equivalent to your code?

public String l337value
{
get {return (ViewState["l337value"] == null)?"":(String)ViewState["l337value"];
}
set { ViewState["l337value"] =value; } }

casting to a string, etc --im assuming the casting stuff's not necessary for VB. And this value will be visible on a Page level, even though its called from a UserControl?

thx btw


I believe that is correct... And yes, it is visible at the Page level.

On load (actually, on Init) I am checking for postback... If it IS a postback, I would add

Dim c = LoadControl("~/urltocontrols/" & ViewState("1337value"))placeholder.Controls.Add(c)

No comments:

Post a Comment