Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Jonathan Eric Miller #1
Possible to create a composite control that has a child control that is a validator that validates the composite control itself?
I am attempting to create a composite control which has a label, followed by
an optional error message, followed by two text boxes. I have everything
working except the optional error message part of it. The optional error
message is a validator that references the composite control itself. I'm
attempting to create this control in the CreateChildControls() method.
However, it isn't working. I'm receiving the following error message. I'm
guessing that this is because, the control hasn't been fully created at that
point, so, the validator can't find it. I'm wondering if anyone else has ran
into this problem and has a workaround? I'm wondering if maybe I can add the
validator in some other method such OnLoad() instead?
[HttpException (0x80004005): Unable to find control id
'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate' property
of ''.]
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AcademicTechnologies.Controls
{
[ValidationPropertyAttribute("Text")]
public class InputZIPCode : Control, INamingContainer
{
private bool required = false;
private Label label1;
private TextBox textBox1;
private TextBox textBox2;
public bool Required
{
set
{
required = value;
}
}
public string Text
{
get
{
EnsureChildControls();
if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
{
return null;
}
string text = textBox1.Text;
if(textBox2.Text.Trim() != "")
{
text += "-" + textBox2.Text;
}
return text;
}
}
protected override void CreateChildControls()
{
label1 = new Label();
label1.Text = "ZIP code: ";
Controls.Add(label1);
System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
if(required == true)
{
RequiredFieldValidator requiredFieldValidator = new
RequiredFieldValidator();
requiredFieldValidator.ControlToValidate = UniqueID;
requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
requiredFieldValidator.ErrorMessage = "Value is required";
Controls.Add(requiredFieldValidator);
}
textBox1 = new TextBox();
textBox1.Width = new Unit(5, UnitType.Em);
textBox1.MaxLength = 5;
Controls.Add(textBox1);
Controls.Add(new LiteralControl("-"));
textBox2 = new TextBox();
textBox2.Width = new Unit(4, UnitType.Em);
textBox2.MaxLength = 4;
Controls.Add(textBox2);
}
}
}
Jon
Jonathan Eric Miller Guest
-
Composite Control - How to Expose a Child Control
I am building a composite control that contains (among other things) a Label control. In order to allow users to set the Text, Font and other... -
Attaching a validator to a composite control
I would like to have my control provide a Required property, and if this property is true, attach a RequiredFieldValidator. This approach works... -
Composite Control - Event not firing in child control
Hello: I am experiencing an issue where I have a composite control (TestOuter) composed of more composite (TestInner) controls. When I am... -
Composite control delegating databound templated features to child <asp:repeater> control
Hi Everybody, I'm creating a composite control in C# that basically renders a bunch of webcontrols such as <asp:Label />, <asp:Repeater />,... -
Using Table control in a custom composite control. Control does not render properly in design time.
All, I have written a very simple custom composite control that includes a control of type System.Web.UI.WebControls.Table. The control... -
Jonathan Eric Miller #2
Re: Possible to create a composite control that has a child control that is a validator that validates the composite control itself?
I tried it in the OnLoad() method as well and the result is the same. I'm
wondering if maybe when it searches for the control, it doesn't include the
parent and only includes child controls?
I'm guessing the answer to this is, no, but, I'm wondering if there is a way
to install the source for ASP.NET so that you can see the actual code that
is being executed?
Jon
"Jonathan Eric Miller" <jemiller@uchicago.edu> wrote in message
news:O7JDJwDcEHA.3716@TK2MSFTNGP11.phx.gbl...by> I am attempting to create a composite control which has a label, followedthat> an optional error message, followed by two text boxes. I have everything
> working except the optional error message part of it. The optional error
> message is a validator that references the composite control itself. I'm
> attempting to create this control in the CreateChildControls() method.
> However, it isn't working. I'm receiving the following error message. I'm
> guessing that this is because, the control hasn't been fully created atran> point, so, the validator can't find it. I'm wondering if anyone else hasthe> into this problem and has a workaround? I'm wondering if maybe I can addproperty> validator in some other method such OnLoad() instead?
>
> [HttpException (0x80004005): Unable to find control id
> 'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate'> of ''.]
>
> using System;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> namespace AcademicTechnologies.Controls
> {
> [ValidationPropertyAttribute("Text")]
> public class InputZIPCode : Control, INamingContainer
> {
> private bool required = false;
> private Label label1;
> private TextBox textBox1;
> private TextBox textBox2;
> public bool Required
> {
> set
> {
> required = value;
> }
> }
> public string Text
> {
> get
> {
> EnsureChildControls();
> if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
> {
> return null;
> }
> string text = textBox1.Text;
> if(textBox2.Text.Trim() != "")
> {
> text += "-" + textBox2.Text;
> }
> return text;
> }
> }
> protected override void CreateChildControls()
> {
> label1 = new Label();
> label1.Text = "ZIP code: ";
> Controls.Add(label1);
> System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
> System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
> System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
> if(required == true)
> {
> RequiredFieldValidator requiredFieldValidator = new
> RequiredFieldValidator();
> requiredFieldValidator.ControlToValidate = UniqueID;
> requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
> requiredFieldValidator.ErrorMessage = "Value is required";
> Controls.Add(requiredFieldValidator);
> }
> textBox1 = new TextBox();
> textBox1.Width = new Unit(5, UnitType.Em);
> textBox1.MaxLength = 5;
> Controls.Add(textBox1);
> Controls.Add(new LiteralControl("-"));
> textBox2 = new TextBox();
> textBox2.Width = new Unit(4, UnitType.Em);
> textBox2.MaxLength = 4;
> Controls.Add(textBox2);
> }
> }
> }
>
> Jon
>
>
Jonathan Eric Miller Guest
-
Jonathan Eric Miller #3
Re: Possible to create a composite control that has a child control that is a validator that validates the composite control itself?
I just read that the controls have to be in the same container, so, I guess
I answered my own question...
Jon
"Jonathan Eric Miller" <jemiller@uchicago.edu> wrote in message
news:uA3Si3DcEHA.3016@tk2msftngp13.phx.gbl...the> I tried it in the OnLoad() method as well and the result is the same. I'm
> wondering if maybe when it searches for the control, it doesn't includeway> parent and only includes child controls?
>
> I'm guessing the answer to this is, no, but, I'm wondering if there is afollowed> to install the source for ASP.NET so that you can see the actual code that
> is being executed?
>
> Jon
>
> "Jonathan Eric Miller" <jemiller@uchicago.edu> wrote in message
> news:O7JDJwDcEHA.3716@TK2MSFTNGP11.phx.gbl...> > I am attempting to create a composite control which has a label,I'm> by> > an optional error message, followed by two text boxes. I have everything
> > working except the optional error message part of it. The optional error
> > message is a validator that references the composite control itself. I'm
> > attempting to create this control in the CreateChildControls() method.
> > However, it isn't working. I'm receiving the following error message.> that> > guessing that this is because, the control hasn't been fully created at> ran> > point, so, the validator can't find it. I'm wondering if anyone else has> the> > into this problem and has a workaround? I'm wondering if maybe I can add> property> > validator in some other method such OnLoad() instead?
> >
> > [HttpException (0x80004005): Unable to find control id
> > 'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate'>> > of ''.]
> >
> > using System;
> > using System.Web.UI;
> > using System.Web.UI.WebControls;
> > namespace AcademicTechnologies.Controls
> > {
> > [ValidationPropertyAttribute("Text")]
> > public class InputZIPCode : Control, INamingContainer
> > {
> > private bool required = false;
> > private Label label1;
> > private TextBox textBox1;
> > private TextBox textBox2;
> > public bool Required
> > {
> > set
> > {
> > required = value;
> > }
> > }
> > public string Text
> > {
> > get
> > {
> > EnsureChildControls();
> > if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
> > {
> > return null;
> > }
> > string text = textBox1.Text;
> > if(textBox2.Text.Trim() != "")
> > {
> > text += "-" + textBox2.Text;
> > }
> > return text;
> > }
> > }
> > protected override void CreateChildControls()
> > {
> > label1 = new Label();
> > label1.Text = "ZIP code: ";
> > Controls.Add(label1);
> > System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
> > System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
> > System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
> > if(required == true)
> > {
> > RequiredFieldValidator requiredFieldValidator = new
> > RequiredFieldValidator();
> > requiredFieldValidator.ControlToValidate = UniqueID;
> > requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
> > requiredFieldValidator.ErrorMessage = "Value is required";
> > Controls.Add(requiredFieldValidator);
> > }
> > textBox1 = new TextBox();
> > textBox1.Width = new Unit(5, UnitType.Em);
> > textBox1.MaxLength = 5;
> > Controls.Add(textBox1);
> > Controls.Add(new LiteralControl("-"));
> > textBox2 = new TextBox();
> > textBox2.Width = new Unit(4, UnitType.Em);
> > textBox2.MaxLength = 4;
> > Controls.Add(textBox2);
> > }
> > }
> > }
> >
> > Jon
> >
> >
>
Jonathan Eric Miller Guest



Reply With Quote

