I'm trying to write a custon control with should interact with other controls in the page referenced by a property. As an example, this property should work like the validators's ControlToValidate property.
If i wrote
public class MyControl : Control {
private Control _ControlToBeUsed;
public Control ControlToBeUsed {
get {return _ControlToBeUsed;}
set {_ControlToBeUsed = value;}
}
So i've created a property of kind control, the designer shows a list of all controls in the page where i can choose which one i would like to attach to this property. Very nice. The aspx looks like this:
However it not works, if i exit the designer, when i come back the property is empty again. And if I tried to run I get one error saying it was not possible to create a code for the type 'System.Web.UI.Control' when load the property ControlToBeUsed.
I've read the Developing Microsoft ASP.NET Server Control and Components by Nikhil Kothari and Vandana Datje but i could not find an example when a property is used to reference an object.
public class MyControl : Control {
private Control _ControlToBeUsed;
public Control ControlToBeUsed {
get {return _ControlToBeUsed;}
set {_ControlToBeUsed = value;}
}
So i've created a property of kind control, the designer shows a list of all controls in the page where i can choose which one i would like to attach to this property. Very nice. The aspx looks like this:
hi! ControlToBeUsed should be a string property taking an identifier of a control. then you have to find reference to control by that identifier using
Parent propert of your custom control.
hi! ControlToBeUsed should be a string property taking an identifier of a control. then you have to find reference to control by that identifier using
Parent propert of your custom control.
hope it helps
orzeh
Yes, That works, Thank you.
But the page developer looses the functionality to choose the selected control from a lookup in the property browser, with the string property the developer has to TYPE the id of the control and he can mistype it easily.
Do you have any suggestion how can i make it works the same way that validators's ControlToValidade do?
Orzeh is right on this. Here is some sample code :
[System.ComponentModel.TypeConverter(typeof(DataSourceIDConverter))]
public String DataSourceID {
get {
String s = (String)ViewState["DataSourceID"];
return (s == null) ? String.Empty : s;
}
set {
ViewState["DataSourceID"] = value;
}
}
protected Control DataSource {
get {
if (DataSourceID == String.Empty)
return null;
else
return NamingContainer.FindControl(DataSourceID);
}
}
With the right TypeConvertor you can show a dropdownlist with only the IDs of the type of controls you want to attach (in this case only DataSource controls).
With the right TypeConvertor you can show a dropdownlist with only the IDs of the type of controls you want to attach (in this case only DataSource controls).
If i'm not asking too much, could you please post that DataSourceIDConverter sample?
DataSourceIDConverter is a TypeConvertor that is part of ASP.NET 2.0, the documentation is available on
MSDN. By the way, if the DataSourceIDConverter or one of the other standard TypeConvertors don't fit your needs, you can inherit from
ControlIDConverter and override the
FilterControl method in which you decide to which controls your custom control can be attached.
When you are not using ASP.NET 2.0, I guess you have to write your own TypeConvertor that searches for all the controls on a page and puts them in a StandardsValuesCollection. But in that case, maybe someone else can help you because I only started working
with ASP.NET with the 2.0 version.
I'm using delphi and I don't have how to migrate to 2.x right now. So I will continue to search for an example of how can i implement this type converter myself.
Anyway, you put me on the right track, and this helps a lot.
Cussuol
Member
36 Points
10 Posts
How to reference other object in a custom properties?
Jan 23, 2006 08:24 PM|LINK
Hi friends
I'm trying to write a custon control with should interact with other controls in the page referenced by a property. As an example, this property should work like the validators's ControlToValidate property.
If i wrote
public class MyControl : Control {
private Control _ControlToBeUsed;
public Control ControlToBeUsed {
get {return _ControlToBeUsed;}
set {_ControlToBeUsed = value;}
}
So i've created a property of kind control, the designer shows a list of all controls in the page where i can choose which one i would like to attach to this property. Very nice. The aspx looks like this:
<cc1:MyControl id="MyControl1" runat="server" controltobeused="choosedcontrol"></cc1:MyControl>
However it not works, if i exit the designer, when i come back the property is empty again. And if I tried to run I get one error saying it was not possible to create a code for the type 'System.Web.UI.Control' when load the property ControlToBeUsed.
I've read the Developing Microsoft ASP.NET Server Control and Components by Nikhil Kothari and Vandana Datje but i could not find an example when a property is used to reference an object.
What should I do?
orzeh
Contributor
4993 Points
897 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 08:33 AM|LINK
hi!
ControlToBeUsed should be a string property taking an identifier of a control. then you have to find reference to control by that identifier using Parent propert of your custom control.
hope it helps
orzeh
Cussuol
Member
36 Points
10 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 10:00 AM|LINK
Yes, That works, Thank you.
But the page developer looses the functionality to choose the selected control from a lookup in the property browser, with the string property the developer has to TYPE the id of the control and he can mistype it easily.
Do you have any suggestion how can i make it works the same way that validators's ControlToValidade do?
srillaert
Member
75 Points
15 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 10:42 AM|LINK
[System.ComponentModel.TypeConverter(typeof(DataSourceIDConverter))]
public String DataSourceID {
get {
String s = (String)ViewState["DataSourceID"];
return (s == null) ? String.Empty : s;
}
set {
ViewState["DataSourceID"] = value;
}
}
protected Control DataSource {
get {
if (DataSourceID == String.Empty)
return null;
else
return NamingContainer.FindControl(DataSourceID);
}
}
With the right TypeConvertor you can show a dropdownlist with only the IDs of the type of controls you want to attach (in this case only DataSource controls).
Have fun.
My Blog
Cussuol
Member
36 Points
10 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 12:18 PM|LINK
If i'm not asking too much, could you please post that DataSourceIDConverter sample?
Thank You
srillaert
Member
75 Points
15 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 03:20 PM|LINK
DataSourceIDConverter is a TypeConvertor that is part of ASP.NET 2.0, the documentation is available on MSDN. By the way, if the DataSourceIDConverter or one of the other standard TypeConvertors don't fit your needs, you can inherit from ControlIDConverter and override the FilterControl method in which you decide to which controls your custom control can be attached.
When you are not using ASP.NET 2.0, I guess you have to write your own TypeConvertor that searches for all the controls on a page and puts them in a StandardsValuesCollection. But in that case, maybe someone else can help you because I only started working with ASP.NET with the 2.0 version.
Hope this helps.
My Blog
Cussuol
Member
36 Points
10 Posts
Re: How to reference other object in a custom properties?
Jan 24, 2006 07:15 PM|LINK
I was afraid you said that.
I'm using delphi and I don't have how to migrate to 2.x right now. So I will continue to search for an example of how can i implement this type converter myself.
Anyway, you put me on the right track, and this helps a lot.
Thank you.