Can't set value to the property "Width" of my custom webcontrol

B

Brook

In webform.aspx: <cc:ctlB ID="ctl1" runat="server" Width="200px" />

If set value to width property, I will get a error that say ctlB.Page is null. This control will be fine with no width property, why?

Could anybody help me ? thanks



ctlA.cs
----------------------------
[
AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ValidationProperty("Text"),
ToolboxData("<{0}:ctlA runat=\"server\"> </{0}:ctlA>")
]
public class ctlA : WebControl, INamingContainer
{
protected TextBox _textBox;
protected HiddenField _hiddenBox;

[Bindable(false), DefaultValue(""), TypeConverter(typeof(UnitConverter))]
public override Unit Width
{
get
{
EnsureChildControls();
return this._textBox.Width;
}
set
{
EnsureChildControls();
this._textBox.Width = value;
}
}

public override bool EnableViewState
{
get
{
EnsureChildControls();
return this._textBox.EnableViewState;
}
set
{
EnsureChildControls();
this._textBox.EnableViewState = value;
this._hiddenBox.EnableViewState = value;
this.EnableViewState = value;
}
}

protected override void CreateChildControls()
{
this._textBox = new TextBox();
this._textBox.ID = "Name";
this._textBox.ReadOnly = this.ReadOnly;
this._hiddenBox = new HiddenField();
this._hiddenBox.ID = "Value";

this.Controls.Add(this._textBox);
this.Controls.Add(this._hiddenBox);
}

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

public ctlA()
: base(HtmlTextWriterTag.Span)
{
}
}


--------------------------------------------------------------------------------

ctlB.cs
------------------------------

[ToolboxData("<{0}:ctlB runat=\"server\"> </{0}:ctlB>")]
public class ctlB : ctlA
{
internal Image _schImage;

public ctlB()
: base()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

this._schImage = new Image();
this._schImage.ID = "imgButton";
this._schImage.Attributes["align"] = "absmiddle";

this._schImage.Attributes["onclick"] = String.Format("__OpenDialog('{0}', null, {1}, {2}, '{3}', '{4}', {5});",
Page.ResolveUrl(this.DialogPagePath), this.DialogWidth.ToString(), this.DialogHeight.ToString(), this._textBox.ClientID, this._hiddenBox.ClientID, (this.DialogMode == DialogMode.Model) ? "true" : "false");

this.Controls.Add(_schImage);
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
 
B

bruce barker

simple, your CreateChildControls references Page before it has been set.
your control should handle being created and its properties set before
being added to the Pages control collection. to work in a designer it
should handle nopage and no request.

defer references to other controls (like the page) until prerender or
render.

-- bruce (sqlwork.com)

In webform.aspx: <cc:ctlB ID="ctl1" runat="server" Width="200px" />

If set value to width property, I will get a error that say ctlB.Page is
null. This control will be fine with no width property, why?

Could anybody help me ? thanks



*ctlA.cs*
*----------------------------*
[
AspNetHostingPermission(SecurityAction.Demand, Level =
AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level =
AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ValidationProperty("Text"),
ToolboxData("<{0}:ctlA runat=\"server\"> </{0}:ctlA>")
]
public class ctlA : WebControl, INamingContainer
{
protected TextBox _textBox;
protected HiddenField _hiddenBox;

[Bindable(false), DefaultValue(""),
TypeConverter(typeof(UnitConverter))]
public override Unit Width
{
get
{
EnsureChildControls();
return this._textBox.Width;
}
set
{
EnsureChildControls();
this._textBox.Width = value;
}
}

public override bool EnableViewState
{
get
{
EnsureChildControls();
return this._textBox.EnableViewState;
}
set
{
EnsureChildControls();
this._textBox.EnableViewState = value;
this._hiddenBox.EnableViewState = value;
this.EnableViewState = value;
}
}

protected override void CreateChildControls()
{
this._textBox = new TextBox();
this._textBox.ID = "Name";
this._textBox.ReadOnly = this.ReadOnly;
this._hiddenBox = new HiddenField();
this._hiddenBox.ID = "Value";

this.Controls.Add(this._textBox);
this.Controls.Add(this._hiddenBox);
}

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

public ctlA()
: base(HtmlTextWriterTag.Span)
{
}
}
------------------------------------------------------------------------
*ctlB.cs*
*------------------------------*

[ToolboxData("<{0}:ctlB runat=\"server\"> </{0}:ctlB>")]
public class ctlB : ctlA
{
internal Image _schImage;

public ctlB()
: base()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

this._schImage = new Image();
this._schImage.ID = "imgButton";
this._schImage.Attributes["align"] = "absmiddle";

this._schImage.Attributes["onclick"] =
String.Format("__OpenDialog('{0}', null, {1}, {2}, '{3}', '{4}', {5});",
Page.ResolveUrl(this.DialogPagePath),
this.DialogWidth.ToString(), this.DialogHeight.ToString(),
this._textBox.ClientID, this._hiddenBox.ClientID, (this.DialogMode ==
DialogMode.Model) ? "true" : "false");

this.Controls.Add(_schImage);
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

this.Page.ClientScript.RegisterClientScriptResource(this.GetType(),
"OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
 
B

Brook

thank you, bruce!
the problem be solved by your help.

But i can't read embeded resource with no page object.

this._schImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(ctlB),
"Search.gif");

How can i get embeded resource in design mode to instead of
GetWebResourceUrl method ?

bruce barker said:
simple, your CreateChildControls references Page before it has been set.
your control should handle being created and its properties set before
being added to the Pages control collection. to work in a designer it
should handle nopage and no request.

defer references to other controls (like the page) until prerender or
render.

-- bruce (sqlwork.com)

In webform.aspx: <cc:ctlB ID="ctl1" runat="server" Width="200px" />

If set value to width property, I will get a error that say ctlB.Page is
null. This control will be fine with no width property, why?

Could anybody help me ? thanks



*ctlA.cs*
*----------------------------*
[
AspNetHostingPermission(SecurityAction.Demand, Level =
AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level =
AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Text"),
ValidationProperty("Text"),
ToolboxData("<{0}:ctlA runat=\"server\"> </{0}:ctlA>")
]
public class ctlA : WebControl, INamingContainer
{
protected TextBox _textBox;
protected HiddenField _hiddenBox;

[Bindable(false), DefaultValue(""),
TypeConverter(typeof(UnitConverter))]
public override Unit Width
{
get
{
EnsureChildControls();
return this._textBox.Width;
}
set
{
EnsureChildControls();
this._textBox.Width = value;
}
}

public override bool EnableViewState
{
get
{
EnsureChildControls();
return this._textBox.EnableViewState;
}
set
{
EnsureChildControls();
this._textBox.EnableViewState = value;
this._hiddenBox.EnableViewState = value;
this.EnableViewState = value;
}
}

protected override void CreateChildControls()
{
this._textBox = new TextBox();
this._textBox.ID = "Name";
this._textBox.ReadOnly = this.ReadOnly;
this._hiddenBox = new HiddenField();
this._hiddenBox.ID = "Value";

this.Controls.Add(this._textBox);
this.Controls.Add(this._hiddenBox);
}

protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

public ctlA()
: base(HtmlTextWriterTag.Span)
{
}
}
------------------------------------------------------------------------
*ctlB.cs*
*------------------------------*

[ToolboxData("<{0}:ctlB runat=\"server\"> </{0}:ctlB>")]
public class ctlB : ctlA
{
internal Image _schImage;

public ctlB()
: base()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();

this._schImage = new Image();
this._schImage.ID = "imgButton";
this._schImage.Attributes["align"] = "absmiddle";

this._schImage.Attributes["onclick"] =
String.Format("__OpenDialog('{0}', null, {1}, {2}, '{3}', '{4}', {5});",
Page.ResolveUrl(this.DialogPagePath),
this.DialogWidth.ToString(), this.DialogHeight.ToString(),
this._textBox.ClientID, this._hiddenBox.ClientID, (this.DialogMode ==
DialogMode.Model) ? "true" : "false");

this.Controls.Add(_schImage);
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

this.Page.ClientScript.RegisterClientScriptResource(this.GetType(),
"OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
 

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

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top