自定义控件怎样侦测到页面上所有控件?

waki 2003-12-31 10:51:10
想实现象验证控件一样的功能,在设计时验证控件可以检测到页面上所有被验证控件,并把它的ID显示出来。是如何做到的?
...全文
40 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lostinet 2003-12-31
  • 打赏
  • 举报
回复
例如这是我做的ComboBox.CustomListID的TypeConverter:

/// <summary>
/// specify the server id of a Control that replace the default list
/// </summary>
[DefaultValue(null)]
[Category("Appearance")]
[TypeConverter(typeof(CustomListIDConverter))]
public string CustomListID
{
get
{
return (string)ViewState["cl"];
}
set
{
ViewState["cl"]=value;
}
}

/// <summary>
/// Converter of CustomListID
/// </summary>
public class CustomListIDConverter:StringConverter
{
/// <summary>
/// false
/// </summary>
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
/// <summary>
/// true
/// </summary>
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
/// <summary>
/// return all designtime Control's ID
/// </summary>
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if(context==null)
return null;
ArrayList al=new ArrayList();
foreach(IComponent ic in context.Container.Components)
{
if(ic is ComboBox)
continue;
if(ic is Control)
{
al.Add( ((Control)ic).ID );
}
}
return new TypeConverter.StandardValuesCollection(al);
}

}
Lostinet 2003-12-31
  • 打赏
  • 举报
回复
Component.Site.Container.Components
houjianxun 2003-12-31
  • 打赏
  • 举报
回复
Page.Controls代表页面中的控件集合
zykj_2000 2003-12-31
  • 打赏
  • 举报
回复
FindControl
glassbottle 2003-12-31
  • 打赏
  • 举报
回复
public class xxxEditor : UITypeEditor
{
private IWindowsFormsEditorService _edSvc = null;

/// <summary>
/// 获取由 EditValue 方法使用的编辑器样式。
/// </summary>
/// <param name="context">可用于获取附加上下文信息的 ITypeDescriptorContext。</param>
/// <returns>UITypeEditorEditStyle 值,指示 EditValue 使用的编辑器的样式。</returns>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
return UITypeEditorEditStyle.DropDown;
return base.GetEditStyle(context);
}

/// <summary>
/// 使用 GetEditStyle 指示的编辑器样式编辑指定的对象值。
/// </summary>
/// <param name="context">可用于获取附加上下文信息的 ITypeDescriptorContext。</param>
/// <param name="provider">IServiceProvider,此编辑器可用其来获取服务。 </param>
/// <param name="value">要编辑的对象。</param>
/// <returns>新的对象值。</returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
ListBox list = new ListBox();

foreach (object item in context.Container.Components)
{
if (item is xxx)
list.Items.Add(((xxx)item).ID);
}
list.BorderStyle = BorderStyle.None;
list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);

if (value != null)
{
int selectedIndex = list.Items.IndexOf(value);
if (selectedIndex >= 0)
list.SelectedIndex = selectedIndex;
}

_edSvc = edSvc;
edSvc.DropDownControl(list);
_edSvc = null;

if (list.SelectedItem != null)
value = list.SelectedItem.ToString();
}
}
return value;
}

/// <summary>
/// 当列表控件的选定项的信息变化时发生的事件。
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void list_SelectedIndexChanged(Object source, EventArgs e)
{
if (_edSvc != null)
_edSvc.CloseDropDown();
}
}
waki 2003-12-31
  • 打赏
  • 举报
回复
要在设计时完成,而不是运行时。
Edison621 2003-12-31
  • 打赏
  • 举报
回复
FindControl?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧