gpt4 book ai didi

c# - 在网格中交互控件

转载 作者:太空狗 更新时间:2023-10-29 21:59:31 25 4
gpt4 key购买 nike

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

我发现为了做到这一点,我必须经常使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com