zoukankan      html  css  js  c++  java
  • 一个遍历页面控件ID并放入DropDown供选择的UITypeEditor

    今天做一个Web控件,其中一个属性是让用户选择页面上的一个DropDownList的ID,为了更好的用户设计体验,我想把控件所在页面的所有的DropDownList控件都遍历出来,然后用户直接选择就可以了。这需要写一个设计器用的类,派生自UITypeEditor,代码如下:

    using System;
    using System.Drawing.Design;
    using System.Web.UI;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    
    namespace MvcApp.WebForms.Controls
    {
    	public class IteratePageControlsUITypeEditor : UITypeEditor
    	{
    		public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    		{
    			return UITypeEditorEditStyle.DropDown;
    		}
    
    		public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
    		{
    			IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
    			ListBox lbValues = new ListBox();
    			lbValues.BorderStyle = BorderStyle.None;
    			IteratePageDropDownList(lbValues, context, value);
    			lbValues.Click += (sender, arg) =>
    			{
    				service.CloseDropDown();
    			};
    			service.DropDownControl(lbValues);
    			if (lbValues.SelectedItem != null)
    				return lbValues.SelectedItem.ToString();
    			else
    				return "";
    		}
    
    		private void IteratePageDropDownList(ListBox lbValues, System.ComponentModel.ITypeDescriptorContext context, object value)
    		{
    			System.Web.UI.WebControls.WebControl self = context.Instance as System.Web.UI.WebControls.WebControl;
    			Page page=null;
    			if (self.Site.Container.Components.Count > 0)
    				page = self.Site.Container.Components[0] as Page;
    			int currentIndex = -1;
    			foreach (System.Web.UI.Control item in page.Controls)
    			{
    				if (item is System.Web.UI.WebControls.DropDownList == false || self.ID == item.ID)
    					continue;
    				currentIndex = lbValues.Items.Add(item.ID);
    				if (item.ID == value.ToString())
    					lbValues.SelectedIndex = currentIndex;
    			}
    		}
    	}
    }
    
     

    使用的时候,只需要在我的控件的属性上声明一下就可以了:

    [Editor(typeof(IteratePageControlsUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string ClientDropDownListID
    {
    	get { return m_ClientDropDownListID; }
    	set { m_ClientDropDownListID = value; }
    }
    

    理解的越多,需要记忆的就越少
  • 相关阅读:
    微信小程序踩坑(二)——微信小程序recorderManager和innerAudioContext相关
    log4j:WARN Please initialize the log4j system properly解决办法
    如何跳过登录验证码
    Fiddler Mock长度变化的response不成功
    解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题
    Fiddler抓不到https的解决办法
    互联网项目流程
    测试人员参与线下问题处理须知
    Mac下安装证书fiddlerRoot.cer
    MySQL存储过程中实现执行动态SQL语句
  • 原文地址:https://www.cnblogs.com/Ricky81317/p/1918152.html
Copyright © 2011-2022 走看看