首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网格中的交互控件

网格中的交互控件
EN

Stack Overflow用户
提问于 2010-11-27 10:11:31
回答 5查看 288关注 0票数 6

我一直在设计一个网页上有一个网格的网站。网格中有多个comboBoxes。这些组合框进行交互。例如,当一个值被用户更改时,另一个值被更改或被禁用/启用等。

我发现为了做到这一点,我不得不经常使用FindControl。就像在一个组合框的selectedindexchanged事件中一样,我需要找到另一个comboBox。

这似乎是一种相当混乱的做事方式。它似乎也让系统暴露在编译器找不到的错误中,比如如果一个组合框的id后来被更改了。

谁能告诉我有没有更好的方法?

EN

回答 5

Stack Overflow用户

发布于 2010-11-30 03:10:31

我有一个web应用程序,它也广泛使用各种FindControl排列,以实现您所描述的。尽管它很脆弱(未经测试就不要更改控件is ),但可以通过一些实用函数使其稍微变得不那么麻烦。下面是我使用的所有FindControl类型的函数--这至少可能对您有所帮助。

代码语言:javascript
复制
/// <summary>
/// Recursively searches for a control within the heirarchy of a given control.
/// </summary>
/// <param name="root">The control to search within.</param>
/// <param name="id">The ID of the control to find.</param>
/// <returns>The Control object of the found control, or null if the control isn't found.</returns>
public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id) return root;

    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null) return t;
    }

    return null;
}

/// <summary>
/// Recursively searches for a control within the heirarchy of a given control using the Client ID
/// of the control. Calling this too early in the lifecycle may not behave as expected.
/// </summary>
/// <param name="root">The control to search within.</param>
/// <param name="clientID">The Client ID of the control to find.</param>
/// <returns>The Control object of the found control, or null if the control isn't found.</returns>
public static Control FindControlRecursiveByClientID(Control root, string clientID)
{
 if (0 == String.Compare(root.ClientID, clientID, true)) return root;

 foreach (Control c in root.Controls)
 {
  Control t = FindControlRecursiveByClientID(c, clientID);
  if (t != null) return t;
 }

 return null;
}

/// <summary>
/// Recursively searches for a group of controls within the heirarchy of a given control tree using the ID
/// of the control.
/// </summary>
/// <param name="root">The control tree to search within.</param>
/// <param name="id">The ID of the control to find.</param>
/// <returns>
/// A collection of the found controls. The collection will be empty if none are found.
/// </returns>
public static List<Control> FindControlsRecursive(Control root, string id)
{
 List<Control> collection = new List<Control>();
 FindControlRecursive(root, id, collection);
 return collection;
}

private static void FindControlRecursive(Control root, string id, List<Control> collection)
{
 foreach (Control c in root.Controls)
 {
  if (0 == String.Compare(c.ID, id, true)) collection.Add(c);
  else FindControlRecursive(c, id, collection);
 }
}

/// <summary>
/// Recursively searches for a control within the heirarchy of a given control using the type
/// of the control.
/// </summary>
/// <typeparam name="T">The type of the control to find.</typeparam>
/// <param name="root">The control to search within.</param>
/// <returns>
/// The Control object of the found control, or null if the control isn't found.
/// </returns>
public static T FindControlRecursiveByType<T>(Control root)
 where T : Control
{
 if (root is T) return (T)root;
 foreach (Control c in root.Controls)
 {
  Control t = FindControlRecursiveByType<T>(c);
  if (t != null) return (T)t;
 }
 return null;
}

/// <summary>
/// Recursively searches for a set of controls within the heirarchy of a given control using the type
/// of the control.
/// </summary>
/// <typeparam name="T">The type of the control to find.</typeparam>
/// <param name="root">The control to search within.</param>
/// <returns>
/// A generic List object containing the controls found, or an empty List of none were found.
/// </returns>
public static List<T> FindControlsRecursiveByType<T>(Control root)
 where T : Control
{
 List<T> collection = new List<T>();
 FindControlRecursiveByType<T>(root, collection);
 return collection;
}

private static void FindControlRecursiveByType<T>(Control root, List<T> collection)
 where T : Control
{
 foreach (Control c in root.Controls)
 {
  if (c is T) collection.Add((T)c);
  else FindControlRecursiveByType<T>(c, collection);
 }
}
票数 1
EN

Stack Overflow用户

发布于 2010-11-28 12:11:47

正是由于这个原因,我从ASP.NET转向使用Silverlight进行开发,并利用了它的MVVM模式。即使使用ASP.Net的GridView项模板,每一项也不能直接绑定到同一模板中的另一项,也不能感知或引用另一项。您的后台代码必须在某种程度上(通常是最完整的)知道视图控件的组成层次结构。

下面是你可以做些什么来更接近“更好的绑定世界”。您仍然可以将您的组合框绑定到相同的列表数据源,但是当创建每一行项目/控件时,您需要将每个项目与一个对象(即标记项目)相关联。然后,在控件的事件处理中,您将检索与引发事件的控件关联的标记项关联的其他控件,并对它们执行您希望执行的操作。

我知道,这不是个好主意,但我想都没想过。也许当我有时间对此进行更多的思考时,我可以更新这篇文章。

票数 0
EN

Stack Overflow用户

发布于 2010-12-01 20:27:02

使用事件来做通知怎么样?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4289749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档