简体   繁体   中英

asp.net custom control property as reference to another control

i am looking to set up a user control that will have 2 public string properties. this string should be the ID of a control of the page with the user control.

after using the control to validate class as one example and the associated control id for a label i have not been able to

  1. have the property display as a drop down with a list of controls on the page in the property panel when adding this control to another page. (this is the way a control to validate work, trying to figure out what i am missing.)
  2. after i am able to get the list of all i am hoping to limit some of these to a specific control type (drop down list or something to that effect). based on some additional reading i have already done i am guessing this will need to be done with a custom type converter .

     [ Category("Behavior"), DefaultValue(""), Description("The State Tex Box"), TypeConverterAttribute(typeof(AssociatedControlConverter)) ] public string StateControlToAutoFill { get { object o = ViewState["StateControlToAutoFill"]; return ((o == null) ? String.Empty : (string)o); } set { ViewState["StateControlToAutoFill"] = value; } } 

using validation controls as a starting point (being that the control to validate prop does what i want for the most part) i was able to solve this doing the following.

  1. took a look at ValidateControlConverter.cs
  2. Created my own LabelOrTextBoxTypeConverter overriding the same method as ValidateControlConverter (FilterControl) with my own logic.

     class LabelOrTextBoxTypeConverter : ControlIDConverter { //public ControlByTypeIDConverter(List<Type> WantedControlTypes) //{ // this.ControlTypes = WantedControlTypes; //} /// <summary> /// /// </summary> /// <param name="control"></param> /// <returns></returns> protected override bool FilterControl(Control control) { bool isWanted = false; foreach (var atype in this.ControlTypes) { isWanted |= control.GetType() == atype; } return isWanted; } public List<Type> ControlTypes { get { return new List<Type>() { typeof(TextBox), typeof(Label) }; } } 

    }

  3. The last step was on the controls property

     [ Category("Target Controls") , DefaultValue("") , Bindable(true) , Description("The State Text Box To Auto Fill.") , TypeConverterAttribute(typeof(LabelOrTextBoxTypeConverter)) ] public string StateControlToAutoFill 

as you can see i didn't get 100% what i wanted here. i was hoping to set this up to just be a Control Filter class and have a list of the types of controls i was interested in passed in. I need to do a little more digging here to complete this but for the time being this is working as expected. anyone who knows the answer to this off the top of their head would be much appreciated.

All said and done this is working. Won't really get to use it to much as you have to be in design mode using the prop panel to take advantage of the generated list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM