Board index » Web Programming » Can't set value to the property "Width" of my custom webcontrol

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

Web Programming66
This is a multi-part message in MIME format.



------=_NextPart_000_003D_01C83334.07BF4270

Content-Type: text/plain;

charset="gb2312"

Content-Transfer-Encoding: quoted-printable



=20

In webform.aspx: <cc:ctlB ID=3D"ctl1" runat=3D"server" Width=3D"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 =3D =

AspNetHostingPermissionLevel.Minimal),

AspNetHostingPermission(SecurityAction.InheritanceDemand, Level =3D =

AspNetHostingPermissionLevel.Minimal),

DefaultProperty("Text"),

ValidationProperty("Text"),

ToolboxData("<{0}:ctlA runat=3D\"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 =3D value;

}

}



public override bool EnableViewState

{

get

{

EnsureChildControls();

return this._textBox.EnableViewState;

}

set

{

EnsureChildControls();

this._textBox.EnableViewState =3D value;

this._hiddenBox.EnableViewState =3D value;

this.EnableViewState =3D value;

}

}



protected override void CreateChildControls()

{

this._textBox =3D new TextBox();

this._textBox.ID =3D "Name";

this._textBox.ReadOnly =3D this.ReadOnly;

this._hiddenBox =3D new HiddenField();

this._hiddenBox.ID =3D "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=3D\"server\"></{0}:ctlB>")]

public class ctlB : ctlA

{

internal Image _schImage;



public ctlB()

: base()

{

}



protected override void CreateChildControls()

{

base.CreateChildControls();



this._schImage =3D new Image();

this._schImage.ID =3D "imgButton";

this._schImage.Attributes["align"] =3D "absmiddle";



this._schImage.Attributes["onclick"] =3D =

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 =

=3D=3D 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;

}

}

}





------=_NextPart_000_003D_01C83334.07BF4270

Content-Type: text/html;

charset="gb2312"

Content-Transfer-Encoding: quoted-printable



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD>

<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">

<META content=3D"MSHTML 6.00.6000.16546" name=3DGENERATOR>

<STYLE></STYLE>

</HEAD><FONT face=3DArial><FONT size=3D2>

<BODY>

<DIV>&nbsp;

<DIV><FONT size=3D3><FONT size=3D2>In webform.aspx:&nbsp; <FONT=20

color=3D#800000>&lt;cc:ctlB ID=3D"ctl1" =

runat=3D"server"&nbsp;Width=3D"200px"=20

/&gt;</FONT></FONT></DIV>

<DIV><FONT size=3D3></FONT>&nbsp;</DIV>

<DIV><FONT size=3D3>If set value to width property, I will get a error =

that say=20

ctlB.Page is null. This control will be fine with no width property,=20

why?</FONT></DIV>

<DIV><FONT size=3D3></FONT>&nbsp;</DIV>

<DIV><FONT size=3D3>Could anybody help me ? thanks</FONT></DIV>

<DIV><FONT size=3D3></FONT>&nbsp;</DIV>

<DIV><FONT size=3D3></FONT>&nbsp;</DIV>

<DIV><FONT size=3D3></FONT>&nbsp;</DIV></FONT>

<DIV><STRONG><FONT size=3D3>ctlA.cs</FONT></STRONG></DIV>

<DIV><STRONG><FONT =

size=3D3>----------------------------</FONT></STRONG></DIV>

<DIV>[<BR>AspNetHostingPermission(SecurityAction.Demand, Level <SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

AspNetHostingPermissionLevel.Minimal),<BR>AspNetHostingPermission(Securit=

yAction.InheritanceDemand,=20

Level </SPAN><SPAN style=3D"COLOR: #000000">=3D</SPAN><SPAN =

style=3D"COLOR: #000000">=20

AspNetHostingPermissionLevel.Minimal),<BR>DefaultProperty(</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">Text</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">),<BR>ValidationProperty(</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">Text</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">),<BR>ToolboxData(</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">&lt;{0}:ctlA=20

runat=3D\"server\"&gt; &lt;/{0}:ctlA&gt;</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">)<BR>]<BR></SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">class</SPAN><SPAN style=3D"COLOR: #000000">=

ctlA :=20

WebControl, INamingContainer<BR>{<BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=

TextBox=20

_textBox;<BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=

HiddenField=20

_hiddenBox;<BR><BR>&nbsp;&nbsp;&nbsp; [Bindable(</SPAN><SPAN=20

style=3D"COLOR: #0000ff">false</SPAN><SPAN style=3D"COLOR: #000000">),=20

DefaultValue(</SPAN><SPAN style=3D"COLOR: #800000">""</SPAN><SPAN=20

style=3D"COLOR: #000000">), TypeConverter(</SPAN><SPAN=20

style=3D"COLOR: #0000ff">typeof</SPAN><SPAN=20

style=3D"COLOR: #000000">(UnitConverter))]<BR>&nbsp;&nbsp;&nbsp; =

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">override</SPAN><SPAN style=3D"COLOR: #000000">=

Unit=20

Width<BR>&nbsp;&nbsp;&nbsp; =

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">get</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">return</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: =

#000000">._textBox.Width;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">set</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._textBox.Width </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

}<BR>&nbsp;&nbsp;&nbsp;=20

}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">override</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">bool</SPAN><SPAN style=3D"COLOR: #000000">=20

EnableViewState<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">get</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">return</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: =

#000000">._textBox.EnableViewState;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=

p;&nbsp;=20

}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">set</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._textBox.EnableViewState </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=

sp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._hiddenBox.EnableViewState </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=

sp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.EnableViewState </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

value;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

}<BR>&nbsp;&nbsp;&nbsp;=20

}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=



</SPAN><SPAN style=3D"COLOR: #0000ff">override</SPAN><SPAN =

style=3D"COLOR: #000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">void</SPAN><SPAN style=3D"COLOR: =

#000000">=20

CreateChildControls()<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">._textBox=20

</SPAN><SPAN style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">new</SPAN><SPAN style=3D"COLOR: =

#000000">=20

TextBox();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">._textBox.ID=20

</SPAN><SPAN style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">Name</SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =



</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._textBox.ReadOnly </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.ReadOnly;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._hiddenBox </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">new</SPAN><SPAN style=3D"COLOR: #000000">=20

HiddenField();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">._hiddenBox.ID=20

</SPAN><SPAN style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">Value</SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: =

#000000">;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.Controls.Add(</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: =

#000000">._textBox);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.Controls.Add(</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._hiddenBox);<BR>&nbsp;&nbsp;&nbsp;=20

}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=



</SPAN><SPAN style=3D"COLOR: #0000ff">override</SPAN><SPAN =

style=3D"COLOR: #000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">void</SPAN><SPAN style=3D"COLOR: =

#000000">=20

Render(HtmlTextWriter writer)<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: #000000">.Render(writer);<BR>&nbsp;&nbsp;&nbsp;=20

}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">override</SPAN><SPAN style=3D"COLOR: #000000">=20

ControlCollection Controls<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">get</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">return</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.Controls;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

}<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=20

ctlA()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </SPAN><SPAN=20

style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: #000000">(HtmlTextWriterTag.Span)<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></SPAN></DIV>

<DIV><SPAN style=3D"COLOR: #000000">

<HR>

</SPAN></DIV>

<DIV><SPAN style=3D"COLOR: #000000"><STRONG><FONT=20

size=3D3>ctlB.cs</FONT></STRONG></SPAN></DIV>

<DIV><SPAN style=3D"COLOR: #000000"><STRONG><FONT=20

size=3D3>------------------------------</FONT></STRONG></SPAN></DIV>

<DIV><SPAN style=3D"COLOR: #000000"></SPAN>&nbsp;</DIV>

<DIV><SPAN style=3D"COLOR: #000000">[ToolboxData(<SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">&lt;{0}:ctlB=20

runat=3D\"server\"&gt; &lt;/{0}:ctlB&gt;</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#000000">)]<BR></SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">class</SPAN><SPAN style=3D"COLOR: #000000">=

ctlB :=20

ctlA<BR>{<BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">internal</SPAN><SPAN style=3D"COLOR: #000000">=

Image=20

_schImage;<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: #000000">=20

ctlB()<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </SPAN><SPAN=20

style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: #000000">()<BR>&nbsp;&nbsp;&nbsp; =

{<BR>&nbsp;&nbsp;&nbsp;=20

}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=



</SPAN><SPAN style=3D"COLOR: #0000ff">override</SPAN><SPAN =

style=3D"COLOR: #000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">void</SPAN><SPAN style=3D"COLOR: =

#000000">=20

CreateChildControls()<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.CreateChildControls();<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=

sp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._schImage </SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #0000ff">new</SPAN><SPAN style=3D"COLOR: #000000">=20

Image();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">._schImage.ID=20

</SPAN><SPAN style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">imgButton</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =



</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._schImage.Attributes[</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">align</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: #000000">] =

</SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=

</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">absmiddle</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: =

#000000">;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._schImage.Attributes[</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: =

#800000">onclick</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN style=3D"COLOR: #000000">] =

</SPAN><SPAN=20

style=3D"COLOR: #000000">=3D</SPAN><SPAN style=3D"COLOR: #000000">=20

String.Format(</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">__OpenDialog('{0}', null, {1}, {2}, '{3}', =

'{4}',=20

{5});</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =



Page.ResolveUrl(</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.DialogPagePath), </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.DialogWidth.ToString(), </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.DialogHeight.ToString(), </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._textBox.ClientID, </SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">._hiddenBox.ClientID, (</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">.DialogMode=20

</SPAN><SPAN style=3D"COLOR: #000000">=3D=3D</SPAN><SPAN style=3D"COLOR: =

#000000">=20

DialogMode.Model) </SPAN><SPAN style=3D"COLOR: #000000">?</SPAN><SPAN=20

style=3D"COLOR: #000000"></SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">true</SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">: </SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">false</SPAN><SPAN style=3D"COLOR: =

#800000">"</SPAN><SPAN=20

style=3D"COLOR: =

#000000">);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: #000000">.Controls.Add(_schImage);<BR>&nbsp;&nbsp;&nbsp; =



}<BR><BR>&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">protected</SPAN><SPAN style=3D"COLOR: #000000">=



</SPAN><SPAN style=3D"COLOR: #0000ff">override</SPAN><SPAN =

style=3D"COLOR: #000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">void</SPAN><SPAN style=3D"COLOR: =

#000000">=20

OnPreRender(EventArgs e)<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.OnPreRender(e);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">this</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.Page.ClientScript.RegisterClientScriptResource(</SPAN><SPAN=20

style=3D"COLOR: #0000ff">this</SPAN><SPAN style=3D"COLOR: =

#000000">.GetType(),=20

</SPAN><SPAN style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #800000">OpenDialog.js</SPAN><SPAN=20

style=3D"COLOR: #800000">"</SPAN><SPAN=20

style=3D"COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp; =

}<BR><BR>&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">public</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">override</SPAN><SPAN =

style=3D"COLOR: #000000">=20

ControlCollection Controls<BR>&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN=20

style=3D"COLOR: #0000ff">get</SPAN><SPAN=20

style=3D"COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

EnsureChildControls();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=

;&nbsp;&nbsp;&nbsp;=20

</SPAN><SPAN style=3D"COLOR: #0000ff">return</SPAN><SPAN style=3D"COLOR: =

#000000">=20

</SPAN><SPAN style=3D"COLOR: #0000ff">base</SPAN><SPAN=20

style=3D"COLOR: =

#000000">.Controls;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20

}<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></SPAN></DIV></SPAN>

<DIV><SPAN=20

style=3D"COLOR: =

#000000">&nbsp;</DIV></SPAN></DIV></BODY></HTML></FONT></FONT>



------=_NextPart_000_003D_01C83334.07BF4270--


-
 

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

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)





Brook wrote:

Quote


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;

}

}

}



-

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

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" <nospam@nospam.com>wrote in message

Quote
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)





Brook wrote:

>

>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;

>}

>}

>}

>



-