J
Jonathan Eric Miller
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
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