How to implement a property like ControlToValidate property?

J

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
 
K

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
 
K

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
 
K

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
 
J

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]â€ç¼–写:
 
J

Jeff

Thanks for your help!
I have solved my problem!

“KMILOâ€ç¼–写:
 
K

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 (e-mail address removed) and if its possible
send me an example.

Thanks in Advance

KMILO



Jeff said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top