J
Jiho Han
I have a custom control - deriving from WebControl - that contains a custom
collection property as the default property.
The collection implements IList and each item is also a custom control
deriving from Panel control.
I don't have a custom collection editor.
Whenever I add my item through the collection editor, the member name
generated by it are "myPanel1", "myPanel2", and so on where the the class
name is MyPanel. And that member name is then put in the codebehind like
so:
protected MyPanel myPanel1;
protected MyPanel myPanel2;
Is there a way to override this behavior? Also, I figured setting the ID
property on the item before it being added to the collection might do the
trick but it didn't. So now I have in my codebehind declarations like above
but in my HTML the thing is persisted like:
<cc:MyPanel Caption="MyPanel1" ID="MyPanel1"></cc:MyPanel>
And c# complains because it's case-sensitive.
Another thing that bothers me more is this -
My main control is derived from WebControl so ParseChildren(true) and
PersistChildren(false) are the default. ParseChild(true, "MyPanels")
actually since, I have the collection as the default property.
So my code looks like this:
[DesignSerializationVisibility(DesignSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
NotifyParentProperty(true)]
public MyPanelCollection MyPanels
{
get
{
if (_myPanels == null)
_myPanels = new MyPanelCollection(this);
return _myPanels;
}
}
It works. Maybe too well. Add 3 panels through the collection editor, it
results in the following HTML.
<cc:MyCustomControl ID="MyCustomControl1" runat="server">
<cc:MyPanel Caption="myPanel1" ID="myPanel1"></cc:MyPanel>
<cc:MyPanel Caption="myPanel2" ID="myPanel2"></cc:MyPanel>
<cc:MyPanel Caption="myPanel3" ID="myPanel3"></cc:MyPanel>
</cc:MyCustomControl>
That's fine, but at the same time, those 3 MyPanels are added as child
controls to my MyCustomControl1 and three of those statements get put into
my codebehind. I go to the HTML view, add a fourth MyPanel following all
conventions except for the number 4. I switch to the design view, open up
the collection editor and now all my members are listed as "TabPage" which
of course do not match any of the statements in my codebehind. And those
three statements are wiped out. I don't know if that's the reason. What
gives?
And I don't understand why my MyPanel controls are being added as child
controls in the first place. I don't add them in my code - actually I was
but then I commented out CreateChildControls. Isn't ParseChildren(true)
supposed to parse the nested content as properties? And so it correctly
parses the default collection and adds individual MyPanel items to the
collection correctly, but why are they added to MyCustomControl as child
controls? Did I confuse anyone or is it just me?
Help.
collection property as the default property.
The collection implements IList and each item is also a custom control
deriving from Panel control.
I don't have a custom collection editor.
Whenever I add my item through the collection editor, the member name
generated by it are "myPanel1", "myPanel2", and so on where the the class
name is MyPanel. And that member name is then put in the codebehind like
so:
protected MyPanel myPanel1;
protected MyPanel myPanel2;
Is there a way to override this behavior? Also, I figured setting the ID
property on the item before it being added to the collection might do the
trick but it didn't. So now I have in my codebehind declarations like above
but in my HTML the thing is persisted like:
<cc:MyPanel Caption="MyPanel1" ID="MyPanel1"></cc:MyPanel>
And c# complains because it's case-sensitive.
Another thing that bothers me more is this -
My main control is derived from WebControl so ParseChildren(true) and
PersistChildren(false) are the default. ParseChild(true, "MyPanels")
actually since, I have the collection as the default property.
So my code looks like this:
[DesignSerializationVisibility(DesignSerializationVisibility.Content),
PersistenceMode(PersistenceMode.InnerDefaultProperty),
NotifyParentProperty(true)]
public MyPanelCollection MyPanels
{
get
{
if (_myPanels == null)
_myPanels = new MyPanelCollection(this);
return _myPanels;
}
}
It works. Maybe too well. Add 3 panels through the collection editor, it
results in the following HTML.
<cc:MyCustomControl ID="MyCustomControl1" runat="server">
<cc:MyPanel Caption="myPanel1" ID="myPanel1"></cc:MyPanel>
<cc:MyPanel Caption="myPanel2" ID="myPanel2"></cc:MyPanel>
<cc:MyPanel Caption="myPanel3" ID="myPanel3"></cc:MyPanel>
</cc:MyCustomControl>
That's fine, but at the same time, those 3 MyPanels are added as child
controls to my MyCustomControl1 and three of those statements get put into
my codebehind. I go to the HTML view, add a fourth MyPanel following all
conventions except for the number 4. I switch to the design view, open up
the collection editor and now all my members are listed as "TabPage" which
of course do not match any of the statements in my codebehind. And those
three statements are wiped out. I don't know if that's the reason. What
gives?
And I don't understand why my MyPanel controls are being added as child
controls in the first place. I don't add them in my code - actually I was
but then I commented out CreateChildControls. Isn't ParseChildren(true)
supposed to parse the nested content as properties? And so it correctly
parses the default collection and adds individual MyPanel items to the
collection correctly, but why are they added to MyCustomControl as child
controls? Did I confuse anyone or is it just me?
Help.