【问题标题】:asp.net custom control property as reference to another controlasp.net 自定义控件属性作为对另一个控件的引用
【发布时间】:2013-03-29 14:00:20
【问题描述】:

我希望设置一个具有 2 个公共字符串属性的用户控件。 这个字符串应该是带有用户控件的页面的控件的ID。

在使用控件验证类作为一个示例以及标签的关联控件 ID 之后,我无法验证

  1. 将此控件添加到另一个页面时,在属性面板中将属性显示为带有页面上控件列表的下拉列表。 (这是验证工作的控件的方式,试图找出我错过了什么。)
  2. 在我能够获得所有列表后,我希望将其中一些限制为特定的控件类型(下拉列表或类似的东西)。根据我已经完成的一些额外阅读,我猜这需要使用自定义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;
        }
    }
    

【问题讨论】:

    标签: c# asp.net class properties


    【解决方案1】:

    使用验证控件作为起点(验证道具的控件在很大程度上完成了我想要的操作)我能够通过以下方式解决这个问题。

    1. 看了ValidateControlConverter.cs
    2. 用我自己的逻辑创建了我自己的 LabelOrTextBoxTypeConverter,覆盖了与 ValidateControlConverter (FilterControl) 相同的方法。

      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. 最后一步是在控件属性上

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

    如您所见,我没有得到 100% 的我想要的东西。 我希望将其设置为只是一个控制过滤器类,并有一个我感兴趣的控件类型列表传入。我需要在这里做更多的挖掘来完成这个,但目前这正在工作正如预期的那样。任何知道这个问题的答案的人都会非常感激。

    言归正传,这是行之有效的。不会真正使用它,因为您必须在设计模式下使用道具面板才能利用生成的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多