Subproperties

J

Justin Friel

Hi,

I've been learning how to create custom web controls and have been
having problems with subproperties.

The first problem is that you can not see subproperties using
intellisense. You can type the subproperty attribute in the tag and it
will render correctly, but the source view of the page shows a
validation error. For example:

<cc1:SubPropertyExample runat="server" textbox-text="Some sample text"
/>

correctly sets the attribute of the sub property, but I get a
validation error. Intellisense does not show Textbox-Text as an
option.

The second issue is that changes made to the property in design view do
not stick. If I set the Text property of the Textbox in design view
the changes are lost as soon as I switch to source view.

Below is a stripped-down sample control that illustrates the problem.
If anyone can tell me what I'm doing wrong I'd appreciate it.

Justin

[ToolboxData("<{0}:SubPropertyExample
runat=server></{0}:SubPropertyExample>")]
public class SubPropertyExample : CompositeControl {
private TextBox textbox;

public SubPropertyExample()
: base() {
}

[
Category("Behavior"),
Bindable(true),
Localizable(true),
NotifyParentProperty(true)
]
public TextBox TextBox {
get {
CreateTextBox();
return textbox;
}
}

protected override void CreateChildControls() {
base.CreateChildControls();
this.CreateTextBox();
}

protected override void RenderContents(HtmlTextWriter output) {
}

private void CreateTextBox() {
if (textbox == null) {
textbox = new TextBox();
textbox.ID = this.GetType().Name;
}
}
}
 
T

Teemu Keiski

Hi,

1. Add
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
attribute to the TextBox property, that makes subproperties visible in
Intellisense

2. I've faced these composite control's refresh problems, and often the
reason for their existence is that MS hasn't implemented
NotifyParentProperty attribute in the so-called simple properties (such as
TextBox's Text). So you could try simply subclassing TextBox

public class MyTextBox : TextBox
{
[NotifyParentProperty(true) ]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}

}

Change your composite control to use it, and see if it then works for the
Text property. If it does, then you know that you need to implement it
yourself for the controls.

Completely another way is to implement the proeprties yourself (as-
top-elevl proeprties) and set them to the child control at render time. That
works, but of course pretty much breaks the idea of using composite controls
(since you need to implement proeprties anyways yourself)
 
J

Justin Friel

Adding
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
to the property fixed the Intellisense problem.

Overriding the text property in an inherited control did not fix the
issue with values set from the designer not persisting. I'm still not
sure what's going on with this.

Thanks for the help,
Justin
 
T

Teemu Keiski

About 2) I forgot to add after adding the new type that change the property
and CreateTextBox to be like this:

public TextBox TextBox {
get {

EnsureChildControls();
return textbox;
}
}

private void CreateTextBox() {
if (textbox == null) {
textbox = new MyTextBox();
textbox.ID = this.GetType().Name;
//Add contrtol to Controls collection
Controls.Add(textbox);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top