Home All Groups Group Topic Archive Search About

Problem with RequiredFieldValidator in custom (C#) control

Author
13 Jan 2006 2:13 PM
Lloyd Dupont
I have a composite control rougly like that::
====
class BlogComments : Control, INamingContainer, IPostBackEventHandler
{
    Panel pAddComment;
    TextBox tTitle;

    protected override void CreateChildControls()
    {
        pAddComment = new Panel();
        Controls.Add(pAddComment);

        pAddComment.Controls.Add(tTitle = new TextBox());
        tTitle.ID = "tTitle";

        RequiredFieldValidator rfv = new RequiredFieldValidator();
        rfv.ErrorMessage = strings.Required;
        rfv.ControlToValidate = "tTitle";
        pAddComment.Controls.Add(rfv);
   }
}
====
Now when I try to display a page (.aspx) where there is a BlogComment I get the following error:
===
[HttpException (0x80004005): Unable to find control id 'tTitle' referenced by the 'ControlToValidate' property of ''.]
   System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +221
   System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +178
   System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() +36
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +46
===
Any tips?

--
I have taken a vow of poverty. If you want to really piss me off, send me money.

Author
13 Jan 2006 2:21 PM
Lloyd Dupont
forget it, bug in my code, I change the ID of tTitle agin one line below...

  "Lloyd Dupont" <net.galador@ld> wrote in message news:%23DAwWuEGGHA.2676@TK2MSFTNGP10.phx.gbl...
  I have a composite control rougly like that::
  ====
  class BlogComments : Control, INamingContainer, IPostBackEventHandler
  {
      Panel pAddComment;
      TextBox tTitle;

      protected override void CreateChildControls()
      {
          pAddComment = new Panel();
          Controls.Add(pAddComment);

          pAddComment.Controls.Add(tTitle = new TextBox());
          tTitle.ID = "tTitle";

          RequiredFieldValidator rfv = new RequiredFieldValidator();
          rfv.ErrorMessage = strings.Required;
          rfv.ControlToValidate = "tTitle";
          pAddComment.Controls.Add(rfv);
     }
  }
  ====
  Now when I try to display a page (.aspx) where there is a BlogComment I get the following error:
  ===
  [HttpException (0x80004005): Unable to find control id 'tTitle' referenced by the 'ControlToValidate' property of ''.]
     System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(String name, String propertyName) +221
     System.Web.UI.WebControls.BaseValidator.ControlPropertiesValid() +178
     System.Web.UI.WebControls.BaseValidator.get_PropertiesValid() +36
     System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +46
  ===
  Any tips?

  --
  I have taken a vow of poverty. If you want to really piss me off, send me money.

AddThis Social Bookmark Button