How to reference other object in a custom properties? http://forums.asp.net/t/956203.aspx/1?How+to+reference+other+object+in+a+custom+properties+Tue, 24 Jan 2006 19:15:00 -05009562031176827http://forums.asp.net/p/956203/1176827.aspx/1?How+to+reference+other+object+in+a+custom+properties+How to reference other object in a custom properties? <p>Hi friends</p> <p>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&nbsp;work like the&nbsp;validators's ControlToValidate property.</p> <p>If i wrote</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;class&nbsp;MyControl&nbsp;:&nbsp;Control&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Control&nbsp;_ControlToBeUsed;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Control&nbsp;ControlToBeUsed&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get&nbsp;{return&nbsp;_ControlToBeUsed;}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set&nbsp;{_ControlToBeUsed&nbsp;=&nbsp;value;}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br> </p> <p>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:</p> <p>&lt;cc1:MyControl id=&quot;MyControl1&quot; runat=&quot;server&quot; controltobeused=&quot;choosedcontrol&quot;&gt;&lt;/cc1:MyControl&gt;</p> <p>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&nbsp;<em>'System.Web.UI.Control' when load the property ControlToBeUsed.</em> </p> <p>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.</p> <p>What should I do?</p> 2006-01-23T20:24:27-05:001177306http://forums.asp.net/p/956203/1177306.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? <blockquote><span class="icon-blockquote"></span> <h4>Cussuol</h4> <p>&nbsp;&nbsp;&nbsp; public&nbsp;class&nbsp;MyControl&nbsp;:&nbsp;Control&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;Control&nbsp;_ControlToBeUsed;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Control&nbsp;ControlToBeUsed&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get&nbsp;{return&nbsp;_ControlToBeUsed;}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set&nbsp;{_ControlToBeUsed&nbsp;=&nbsp;value;}<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br> </p> <p>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:</p> <p>&lt;cc1:MyControl id=&quot;MyControl1&quot; runat=&quot;server&quot; controltobeused=&quot;choosedcontrol&quot;&gt;&lt;/cc1:MyControl&gt;</p> </blockquote> <br> <br> hi!<br> <i>ControlToBeUsed</i> should be a string property taking an identifier of a control. then you have to find reference to control by that identifier using <i>Parent</i> propert of your custom control.<br> <br> hope it helps<br> <br> orzeh<br> 2006-01-24T08:33:57-05:001177349http://forums.asp.net/p/956203/1177349.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>orzeh</h4> hi!<br> <i>ControlToBeUsed</i> should be a string property taking an identifier of a control. then you have to find reference to control by that identifier using <i>Parent</i> propert of your custom control.<br> <br> hope it helps<br> <br> orzeh<br> </blockquote> <p></p> <p>Yes, That works, Thank you.</p> <p>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&nbsp;he can mistype it easily.</p> <p>Do you have any suggestion how can i make it works the same way that validators's ControlToValidade do?</p> 2006-01-24T10:00:18-05:001177368http://forums.asp.net/p/956203/1177368.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? Orzeh is right on this. Here is some sample code :<br> <br> [System.ComponentModel.TypeConverter(typeof(DataSourceIDConverter))]<br> public String DataSourceID {<br> &nbsp;&nbsp;&nbsp; get {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String s = (String)ViewState[&quot;DataSourceID&quot;];<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (s == null) ? String.Empty : s;<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp; set {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewState[&quot;DataSourceID&quot;] = value;<br> &nbsp;&nbsp;&nbsp; }<br> }<br> <br> protected Control DataSource {<br> &nbsp;&nbsp;&nbsp; get {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (DataSourceID == String.Empty)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NamingContainer.FindControl(DataSourceID);<br> &nbsp;&nbsp;&nbsp; }<br> }<br> <br> 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).<br> <br> <br> Have fun.<br> 2006-01-24T10:42:53-05:001177426http://forums.asp.net/p/956203/1177426.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>srillaert</h4> 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).<br> </blockquote> <p></p> <p>If i'm not asking too much, could you please post that DataSourceIDConverter sample?</p> <p>Thank You </p> 2006-01-24T12:18:26-05:001177673http://forums.asp.net/p/956203/1177673.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? Hi Cussuol,<br> <br> DataSourceIDConverter is a TypeConvertor that is part of ASP.NET 2.0, the documentation is available on <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.design.webcontrols.datasourceidconverter.aspx"> MSDN</a>. By the way, if the DataSourceIDConverter or one of the other standard TypeConvertors don't fit your needs, you can inherit from <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.controlidconverter.aspx"> ControlIDConverter </a>and override the <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.controlidconverter.filtercontrol.aspx"> FilterControl </a>method in which you decide to which controls your custom control can be attached.<br> <br> 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.<br> <br> Hope this helps. 2006-01-24T15:20:29-05:001178063http://forums.asp.net/p/956203/1178063.aspx/1?Re+How+to+reference+other+object+in+a+custom+properties+Re: How to reference other object in a custom properties? <p>I was afraid you said that.</p> <p>I'm using delphi and I don't have how to&nbsp;migrate to 2.x right now. So I will continue to search for an example of how can i implement this type converter myself.</p> <p>Anyway, you put me on the right track, and this helps a lot.</p> <p>Thank you.</p> 2006-01-24T19:15:00-05:00