Home All Groups Group Topic Archive Search About

How to implement a property like ControlToValidate property?

Author
27 Apr 2005 3:26 AM
Jeff
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

Author
27 Apr 2005 8:51 AM
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

Show quoteHide quote
"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
Author
28 Apr 2005 1:15 PM
KMILO
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

Show quoteHide quote
"Kannan.V [MCSD.net]" <KannanVMCSD***@discussions.microsoft.com> wrote in message news:90548606-9DF5-463B-A086-94BC0F1D4F9B@microsoft.com...
> 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
Author
30 Apr 2005 5:23 AM
Jeff
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]”编写:

Show quoteHide quote
> 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
Author
28 Apr 2005 1:09 PM
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
Author
30 Apr 2005 6:38 AM
Jeff
Thanks for your help!
I have solved my problem!

“KMILO”编写:

Show quoteHide quote
> 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
Author
2 May 2005 1:49 PM
KMILO
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 kmil***@gmail.com and if its possible
send me an example.

Thanks in Advance

KMILO



Show quoteHide quote
"Jeff" <week***@gmail.com> wrote in message
news:EE0FF5D4-A178-4272-9E9F-D3AAA669DD2F@microsoft.com...
> 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