How to implement a property like ControlToValidate property?

Discussion in 'ASP .Net Web Controls' started by Jeff, Apr 27, 2005.

  1. Jeff

    Jeff Guest

    I want to implement a property in a custom web control. The property
    is like ControlToValidate property of standard validators.

    My problem is how to implement the design-time function of this property.
    ControlToValidate in the property window will show a dropdown list which
    contains all controls that can be validated in the current page.

    I don't know how to find all controls that can be validated in design-time.

    Who can help me? Thanks
     
    Jeff, Apr 27, 2005
    #1
    1. Advertising

  2. hi Jeff,

    create a property of the datatype that you wanted to be listed in the
    dropdown in the property browser.
    For example the code below lists all textboxes on the page in the return
    MyControltoValidate property.

    EXAMPLE:

    private TextBox _MyControltoValidate;
    public TextBox MyControltoValidate
    {
    get
    {
    return _MyControltoValidate;
    }
    set
    {
    _MyControltoValidate = value;

    }
    }

    Include the above code in ur control, drop the control on to your page.
    Draw some textboxes also, now when you open this property all the textboxes
    on the page will be displayed.

    Hope this helped you,
    Regds
    Kannan.V

    "Jeff" wrote:

    > I want to implement a property in a custom web control. The property
    > is like ControlToValidate property of standard validators.
    >
    > My problem is how to implement the design-time function of this property.
    > ControlToValidate in the property window will show a dropdown list which
    > contains all controls that can be validated in the current page.
    >
    > I don't know how to find all controls that can be validated in design-time.
    >
    > Who can help me? Thanks
     
    Kannan.V [MCSD.net], Apr 27, 2005
    #2
    1. Advertising

  3. Jeff

    KMILO Guest

    Im doing the same thing as you, but the following error appears "Unable to generate code for a value of type 'System.Web.UI.HtmlControls.HtmlInputHidden'. This error occurred while trying to generate the property value for CajaDeTexto"
    but I solve the situation implementing this typeconverter to a String property:

    #region TypeConverter
    public class HtmlControlConverter : StringConverter
    {
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
    return true;
    }
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
    return false;
    }
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
    if ((context == null) || (context.Container == null))
    {
    return null;
    }
    Object[] serverControls = this.GetControls(context.Container);
    if (serverControls != null)
    {
    return new StandardValuesCollection(serverControls);
    }
    return null;
    }
    private object[] GetControls(IContainer container)
    {
    ArrayList availableControls = new ArrayList();
    foreach( IComponent component in container.Components )
    {
    Control serverControl = component as Control;
    if ( serverControl != null &&
    !(serverControl is Page) &&
    serverControl.ID != null &&
    serverControl.ID.Length != 0 &&
    IncludeControl(serverControl)
    )
    {
    availableControls.Add(serverControl.ID);
    }
    }
    availableControls.Sort(Comparer.Default);
    return availableControls.ToArray();
    }
    private bool IncludeControl(Control serverControl)
    {
    bool ReturnedVal = false;
    string ControlType = serverControl.GetType().ToString();
    switch(ControlType)
    {
    case "System.Web.UI.HtmlControls.HtmlInputHidden":
    ReturnedVal = true;
    break;
    case "System.Web.UI.HtmlControls.HtmlGenericControl":
    ReturnedVal = true;
    break;
    case "System.Web.UI.HtmlControls.HtmlInputText":
    ReturnedVal = true;
    break;
    case "System.Web.UI.HtmlControls.HtmlTable":
    ReturnedVal = true;
    break;
    }
    return ReturnedVal;
    }
    }
    #endregion

    And the implementation looks like this:

    #region TypeConverterImplementation
    private string _cajadetexto;
    [Bindable(false),Category("Controls"),TypeConverter(typeof(HtmlControlConverter))]
    //,EditorAttribute(typeof(System.Web.UI.Design.HtmlControlDesigner), typeof(System.Web.UI.HtmlControls.HtmlInputHidden))]
    public virtual string CajaDeTexto
    {
    get {return _cajadetexto;}
    set {_cajadetexto = value;}
    }
    #endregion

    This was a easy way to get out the trouble, if you knows any way better let me know it....

    Cheers

    KMILO
     
    KMILO, Apr 28, 2005
    #3
  4. Jeff

    KMILO Guest

    Hi Kannan,

    I was in the same trouble as jeff, but my controls are htmlcontrols with runat server attribute, but when I put my control into a datalist the following error appears

    Unable to generate code for a value of type 'System.Web.UI.HtmlControls.HtmlInputHidden'. This error occurred while trying to generate the property value for CajaDeTexto.

    the code that Im using is:


    private HtmlInputHidden _cajadetexto;
    [Bindable(false),Category("Data"),TypeConverter(typeof(System.Web.UI.HtmlControls.HtmlInputHidden))]
    public virtual HtmlInputHidden CajaDeTexto
    {
    get {return _cajadetexto;}
    set {_cajadetexto = value;}
    }

    I dont know if my trouble is the typeconverter, I solve this situation like I posted before, but I still want to know how I can get the control directly and using the property grid as well

    Thanks In Advance

    KMILO

    "Kannan.V [MCSD.net]" <> wrote in message news:...
    > hi Jeff,
    >
    > create a property of the datatype that you wanted to be listed in the
    > dropdown in the property browser.
    > For example the code below lists all textboxes on the page in the return
    > MyControltoValidate property.
    >
    > EXAMPLE:
    >
    > private TextBox _MyControltoValidate;
    > public TextBox MyControltoValidate
    > {
    > get
    > {
    > return _MyControltoValidate;
    > }
    > set
    > {
    > _MyControltoValidate = value;
    >
    > }
    > }
    >
    > Include the above code in ur control, drop the control on to your page.
    > Draw some textboxes also, now when you open this property all the textboxes
    > on the page will be displayed.
    >
    > Hope this helped you,
    > Regds
    > Kannan.V
    >
    > "Jeff" wrote:
    >
    >> I want to implement a property in a custom web control. The property
    >> is like ControlToValidate property of standard validators.
    >>
    >> My problem is how to implement the design-time function of this property.
    >> ControlToValidate in the property window will show a dropdown list which
    >> contains all controls that can be validated in the current page.
    >>
    >> I don't know how to find all controls that can be validated in design-time.
    >>
    >> Who can help me? Thanks
     
    KMILO, Apr 28, 2005
    #4
  5. Jeff

    Jeff Guest

    Thank you for your reply.
    But I'm afraid the property's type must be string and all controls in the
    Page that canbe validated (like textbox,dropdownlist etc)will automatically
    showed in the dropdown list. The user can set the property in the property
    window in two ways: 1.Directly input the control name or 2.Select a certain
    control in the dropdownlist.
    Can you give me other advice on this problem?

    “Kannan.V [MCSD.net]â€ç¼–写:

    > hi Jeff,
    >
    > create a property of the datatype that you wanted to be listed in the
    > dropdown in the property browser.
    > For example the code below lists all textboxes on the page in the return
    > MyControltoValidate property.
    >
    > EXAMPLE:
    >
    > private TextBox _MyControltoValidate;
    > public TextBox MyControltoValidate
    > {
    > get
    > {
    > return _MyControltoValidate;
    > }
    > set
    > {
    > _MyControltoValidate = value;
    >
    > }
    > }
    >
    > Include the above code in ur control, drop the control on to your page.
    > Draw some textboxes also, now when you open this property all the textboxes
    > on the page will be displayed.
    >
    > Hope this helped you,
    > Regds
    > Kannan.V
    >
    > "Jeff" wrote:
    >
    > > I want to implement a property in a custom web control. The property
    > > is like ControlToValidate property of standard validators.
    > >
    > > My problem is how to implement the design-time function of this property.
    > > ControlToValidate in the property window will show a dropdown list which
    > > contains all controls that can be validated in the current page.
    > >
    > > I don't know how to find all controls that can be validated in design-time.
    > >
    > > Who can help me? Thanks
     
    Jeff, Apr 30, 2005
    #5
  6. Jeff

    Jeff Guest

    Thanks for your help!
    I have solved my problem!

    “KMILOâ€ç¼–写:

    > Im doing the same thing as you, but the following error appears "Unable to generate code for a value of type 'System.Web.UI.HtmlControls.HtmlInputHidden'. This error occurred while trying to generate the property value for CajaDeTexto"
    > but I solve the situation implementing this typeconverter to a String property:
    >
    > #region TypeConverter
    > public class HtmlControlConverter : StringConverter
    > {
    > public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    > {
    > return true;
    > }
    > public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    > {
    > return false;
    > }
    > public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    > {
    > if ((context == null) || (context.Container == null))
    > {
    > return null;
    > }
    > Object[] serverControls = this.GetControls(context.Container);
    > if (serverControls != null)
    > {
    > return new StandardValuesCollection(serverControls);
    > }
    > return null;
    > }
    > private object[] GetControls(IContainer container)
    > {
    > ArrayList availableControls = new ArrayList();
    > foreach( IComponent component in container.Components )
    > {
    > Control serverControl = component as Control;
    > if ( serverControl != null &&
    > !(serverControl is Page) &&
    > serverControl.ID != null &&
    > serverControl.ID.Length != 0 &&
    > IncludeControl(serverControl)
    > )
    > {
    > availableControls.Add(serverControl.ID);
    > }
    > }
    > availableControls.Sort(Comparer.Default);
    > return availableControls.ToArray();
    > }
    > private bool IncludeControl(Control serverControl)
    > {
    > bool ReturnedVal = false;
    > string ControlType = serverControl.GetType().ToString();
    > switch(ControlType)
    > {
    > case "System.Web.UI.HtmlControls.HtmlInputHidden":
    > ReturnedVal = true;
    > break;
    > case "System.Web.UI.HtmlControls.HtmlGenericControl":
    > ReturnedVal = true;
    > break;
    > case "System.Web.UI.HtmlControls.HtmlInputText":
    > ReturnedVal = true;
    > break;
    > case "System.Web.UI.HtmlControls.HtmlTable":
    > ReturnedVal = true;
    > break;
    > }
    > return ReturnedVal;
    > }
    > }
    > #endregion
    >
    > And the implementation looks like this:
    >
    > #region TypeConverterImplementation
    > private string _cajadetexto;
    > [Bindable(false),Category("Controls"),TypeConverter(typeof(HtmlControlConverter))]
    > //,EditorAttribute(typeof(System.Web.UI.Design.HtmlControlDesigner), typeof(System.Web.UI.HtmlControls.HtmlInputHidden))]
    > public virtual string CajaDeTexto
    > {
    > get {return _cajadetexto;}
    > set {_cajadetexto = value;}
    > }
    > #endregion
    >
    > This was a easy way to get out the trouble, if you knows any way better let me know it....
    >
    > Cheers
    >
    > KMILO
     
    Jeff, Apr 30, 2005
    #6
  7. Jeff

    KMILO Guest

    Can you tell me how do you solve this situation, I guess that you finally do
    what you want (Implement directly the controls, without a string property)
    if you want write me to this email and if its possible
    send me an example.

    Thanks in Advance

    KMILO



    "Jeff" <> wrote in message
    news:...
    > Thanks for your help!
    > I have solved my problem!
    >
    > "KMILO"??:
    >
    >> Im doing the same thing as you, but the following error appears "Unable
    >> to generate code for a value of type
    >> 'System.Web.UI.HtmlControls.HtmlInputHidden'. This error occurred while
    >> trying to generate the property value for CajaDeTexto"
    >> but I solve the situation implementing this typeconverter to a String
    >> property:
    >>
    >> #region TypeConverter
    >> public class HtmlControlConverter : StringConverter
    >> {
    >> public override bool GetStandardValuesSupported(ITypeDescriptorContext
    >> context)
    >> {
    >> return true;
    >> }
    >> public override bool GetStandardValuesExclusive(ITypeDescriptorContext
    >> context)
    >> {
    >> return false;
    >> }
    >> public override StandardValuesCollection
    >> GetStandardValues(ITypeDescriptorContext context)
    >> {
    >> if ((context == null) || (context.Container == null))
    >> {
    >> return null;
    >> }
    >> Object[] serverControls = this.GetControls(context.Container);
    >> if (serverControls != null)
    >> {
    >> return new StandardValuesCollection(serverControls);
    >> }
    >> return null;
    >> }
    >> private object[] GetControls(IContainer container)
    >> {
    >> ArrayList availableControls = new ArrayList();
    >> foreach( IComponent component in container.Components )
    >> {
    >> Control serverControl = component as Control;
    >> if ( serverControl != null &&
    >> !(serverControl is Page) &&
    >> serverControl.ID != null &&
    >> serverControl.ID.Length != 0 &&
    >> IncludeControl(serverControl)
    >> )
    >> {
    >> availableControls.Add(serverControl.ID);
    >> }
    >> }
    >> availableControls.Sort(Comparer.Default);
    >> return availableControls.ToArray();
    >> }
    >> private bool IncludeControl(Control serverControl)
    >> {
    >> bool ReturnedVal = false;
    >> string ControlType = serverControl.GetType().ToString();
    >> switch(ControlType)
    >> {
    >> case "System.Web.UI.HtmlControls.HtmlInputHidden":
    >> ReturnedVal = true;
    >> break;
    >> case "System.Web.UI.HtmlControls.HtmlGenericControl":
    >> ReturnedVal = true;
    >> break;
    >> case "System.Web.UI.HtmlControls.HtmlInputText":
    >> ReturnedVal = true;
    >> break;
    >> case "System.Web.UI.HtmlControls.HtmlTable":
    >> ReturnedVal = true;
    >> break;
    >> }
    >> return ReturnedVal;
    >> }
    >> }
    >> #endregion
    >>
    >> And the implementation looks like this:
    >>
    >> #region TypeConverterImplementation
    >> private string _cajadetexto;
    >> [Bindable(false),Category("Controls"),TypeConverter(typeof(HtmlControlConverter))]
    >> //,EditorAttribute(typeof(System.Web.UI.Design.HtmlControlDesigner),
    >> typeof(System.Web.UI.HtmlControls.HtmlInputHidden))]
    >> public virtual string CajaDeTexto
    >> {
    >> get {return _cajadetexto;}
    >> set {_cajadetexto = value;}
    >> }
    >> #endregion
    >>
    >> This was a easy way to get out the trouble, if you knows any way better
    >> let me know it....
    >>
    >> Cheers
    >>
    >> KMILO
     
    KMILO, May 2, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Nagy

    Custom Control needs ControlToValidate

    Steven Nagy, Mar 6, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    596
    MasterGaurav \(www.edujini-labs.com\)
    May 8, 2007
  2. DaveyP
    Replies:
    1
    Views:
    678
    Chris Shepherd
    Apr 18, 2008
  3. Nathan Sokalski
    Replies:
    0
    Views:
    353
    Nathan Sokalski
    Mar 8, 2010
  4. TS

    controlToValidate property of compareValidator

    TS, Dec 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    200
    Steven Cheng[MSFT]
    Jan 3, 2006
  5. TS
    Replies:
    7
    Views:
    255
Loading...

Share This Page