473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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


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.Inheritance Demand, 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.RegisterClientScriptResourc e(this.GetType(), "OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
Nov 30 '07 #1
2 1767
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.Inheritance Demand, 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.RegisterClientScriptResourc e(this.GetType(),
"OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
Nov 30 '07 #2
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" <no****@nospam.comwrote in message
news:ec**************@TK2MSFTNGP05.phx.gbl...
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.Inheritanc eDemand, 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.RegisterClientScriptResour ce(this.GetType(),
"OpenDialog.js");
}

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
}
Dec 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Saqib Ali | last post by:
Howdy Folks, I have a b/w fotograf that i am displaying on a website using the width="100%" attribute so that it spans the width of the web browser The rosolution of the picture is 1296 x 204....
8
by: Mario T. Lanza | last post by:
I'm not sure what I'm doing wrong. I have a form that has mnay input fields. Before each input field is a label enclosed in custom LABEL tags. Inside my CSS I have: LABEL { width: 120px; }
6
by: Joe Morrison | last post by:
By my reading of the HTML standard: http://www.w3.org/TR/REC-html40/struct/tables.html the HTML shown below should display the text "Enter password" with the input field immediately adjacent,...
15
by: Gérard Talbot | last post by:
Hello all, I'd like to know and understand the difference between, say, <img src="/ImageFilename.png" width="123" height="456" alt=""> and <img src="/ImageFilename.png" style="width:...
2
by: Brian Bischof | last post by:
To get my formatting to work with Firefox and IE browsers, I have to be very careful about the layout. So I'm removing all the Style tags that set the Height and Width values (and some of these...
1
by: Nathan Sokalski | last post by:
I am trying to convert <td width="*"to CSS, but all the sources I could find that give the syntax for the CSS width property say that you must specify a length, percentage, auto, or inherit. I also...
3
by: Phil Endecott | last post by:
Dear Experts, It looks as if the HTML4 spec does not define a meaning for empty height and width attributes in an IMG element. Moz seems to ignore them, while IE7 sets the dimension to 1 pixel...
7
by: Christian Hackl | last post by:
Hi everyone, I've got a question about what makes the "img" element's width/height attributes valid HTML or XHTML. First of all, this is a rather theoretical question, but digging through the...
3
by: Tom | last post by:
Can someone please explain the non-working aspect of binding to Width? See code below. Thanks !! -- Tom =================================================== using System; using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.