Custom Control needs ControlToValidate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Nagy

    #1

    Custom Control needs ControlToValidate

    Hi all,

    I have a custom user control in an ASP.NET app (.NET 2.0).
    This control needs a public property called "ControlToValid ate" which
    accepts strings.
    I need to be able to declare it much as you would with a validator
    control, like this:

    <asp:Textbox id="myTextbox" runat="server" />
    <uc1:MyContro l id="mc1" runat="server" ControlToValida te="myTextbox" /
    >
    What is important is that my custom control is able to get the
    ClientID of the control passed in.
    The above code would only pass "myTextbox" as a string, not the actual
    textbox control.

    I am curious how to do this. If I were to user a
    RequiredFieldVa lidator, I could set ControlToValida te as per my above
    usage. So its this behaviour I want to replicate.

    I used reflector to view the RequiredFieldVa lidator's implementation
    of ControlToValida te and it simply inherits from BaseValidator. I
    tried using this inheritance also, but kept getting compile errors
    because BaseValidator ultimately inherits from Control, not
    UserControl (sif that should matter).

    Googling brought me no love. I suspect there is an attribute I need to
    use, possibly a TypeConverter somewhere, just not sure how.

    Any help would be greatly appreciated.

    Cheers,
    Steven

  • MasterGaurav \(www.edujini-labs.com\)

    #2
    Re: Custom Control needs ControlToValida te

    What is important is that my custom control is able to get the
    ClientID of the control passed in.
    The above code would only pass "myTextbox" as a string, not the actual
    textbox control.

    Control ctrl = base.NamingCont ainer.FindContr ol(this.Control ToValidate);
    if(ctrl != null)
    {
    TextBox box = ctrl as TextBox;
    if(box != null)
    {
    string value = box.Text;
    }
    }


    HTH

    --
    Happy Hacking,
    Gaurav Vaish | www.mastergaurav.com


    -----------------------------------------



    Comment

    • Steven Nagy

      #3
      Re: Custom Control needs ControlToValida te

      On Mar 7, 4:33 am, "MasterGaur av \(www.edujini-labs.com\)"
      <gaurav.vaish.n os...@nospam.gm ail.comwrote:
      What is important is that my custom control is able to get the
      ClientID of the control passed in.
      The above code would only pass "myTextbox" as a string, not the actual
      textbox control.
      >
      Control ctrl = base.NamingCont ainer.FindContr ol(this.Control ToValidate);
      if(ctrl != null)
      {
      TextBox box = ctrl as TextBox;
      if(box != null)
      {
      string value = box.Text;
      }
      >
      }
      >
      HTH
      >
      --
      Happy Hacking,
      Gaurav Vaish |https://www.mastergaurav.comwww.eduj...ujini-labs.com
      -----------------------------------------
      Ok so in your example, the string that gets passed is something like
      "txtUserNam e".
      However, at runtime the textbox id is actually
      "MainForm_SomeS ubConrol_txtUse rName".
      Will this code still work 100% if the form has two "txtUserNam e"
      textboxes?
      I haven't used "base.NamingCon tainer" before; is this the container
      that is holding the current control?
      How would your example work in this case:

      <asp:Panel id="pnlSomeStuf f" runat="server">
      <asp:Textbox id="txtUserName " runat="server" />
      </asp:Panel>
      <uc1:MyContro l id="mc" runat="server" ControlToValida te="txtUserName " /
      >
      .... because the textbox is not in the same scope as the custom
      control.

      I appreciate your help,
      Steven Nagy

      Comment

      • Steven Nagy

        #4
        Re: Custom Control needs ControlToValida te

        Anyone?

        Comment

        • MasterGaurav \(www.edujini-labs.com\)

          #5
          Re: Custom Control needs ControlToValida te

          Ok so in your example, the string that gets passed is something like
          "txtUserNam e".
          However, at runtime the textbox id is actually
          "MainForm_SomeS ubConrol_txtUse rName".
          Will this code still work 100% if the form has two "txtUserNam e"
          textboxes?
          I missed out your "Anyone?" posting...

          The textbox-ID is never "MainForm_SomeS ubControl_txtUs erName". It is the
          value of "ClientID".
          I haven't used "base.NamingCon tainer" before; is this the container
          that is holding the current control?
          Yes, NamingContainer is the container holding the current control.
          How would your example work in this case:
          >
          <asp:Panel id="pnlSomeStuf f" runat="server">
          <asp:Textbox id="txtUserName " runat="server" />
          </asp:Panel>
          <uc1:MyContro l id="mc" runat="server" ControlToValida te="txtUserName " /
          >>
          >
          ... because the textbox is not in the same scope as the custom
          control.
          In this case, you can use (from inside uc1:MyControl):

          this.Page.FindC ontrol("txtUser Name")

          Note that UserControl also implements INamingContaine r. So, for the
          uc1-control, the lookup would be within MyControl since that's the
          NamingContainer for the control.

          Btw, for all these complex-resolving issues, I would recommend that you
          design a WebControl rather than UserControl (because UserControl becomes the
          naming container).

          Anyway, Page.FindContro l("...") should give you the expected results.


          --
          Happy Hacking,
          Gaurav Vaish | www.mastergaurav.com


          -----------------------------------------


          Comment

          Working...