c# - 在网格中交互控件

标签 c# asp.net

我一直在设计一个页面上有网格的网站。网格中有多个组合框。这些组合框相互作用。即当一个值被用户更改时,另一个值发生变化或被禁用/启用等。

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

这似乎是一种相当困惑的做事方式。它似乎也让系统容易出现错误,编译器不会发现组合框是否在以后更改了它的 ID。

有人能告诉我有没有更好的方法来解决这个问题?

最佳答案

我有一个网络应用程序,它还广泛使用各种 FindControl 排列来完成您描述的内容。虽然它很脆弱(不要在未经测试的情况下更改控件 ID),但可以通过一些实用函数使其稍微不那么麻烦。以下是我使用的所有 FindControl 类型的函数——这至少可以帮助您。

/// <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);
 }
}

关于c# - 在网格中交互控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4289749/

相关文章:

c# - Web Api授权

c# - 模块化网站设计,使用 ASP.NET MVC,我想要一个不那么单一的设计

c# - MVC 4 绑定(bind)到子表列表

c# - 从 WCF 服务启动多个任务

c# - 从 ListView 控件获取索引项

c# - 使用 C# Cheat Engine 中的指针

c# - 如何删除CSS类

c# - .net 桌面应用程序 - 如何存储/更改应用程序设置或连接字符串?

javascript - ajax 调用成功后更改 @Html.DisplayFor 项目

jquery - 如何使用 jQuery 设置文本标签?