Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting controls within a Grid

Tags:

c#

asp.net

I've been designing a website which has a grid on the page. The grid has multiple comboBoxes within it. These comboboxes interact. ie When one value is changed by the user another one has it's value change or is disabled/enabled etc.

I find in order to do this I have to use FindControl a lot. Like in the selectedindexchanged event of one combobox I'll need to find another comboBox.

This seems rather a messy way of doing things. It also seems like it leaves the system open to errors the compiler won't find say if a combobox has it's id changed later on down the line.

Can someone tell me is there a better way of going about this?

like image 288
MRA Avatar asked Nov 27 '10 02:11

MRA


People also ask

What is meant by grid control?

A grid control is similar to a spreadsheet. Use grids to display data and to enable users to enter information. Unlike an edit control, grid controls can show multiple data items and multiple table rows at once. You also can use grid controls to enable users to edit table records.

What are the uses of grid control in data process explain?

A common use of the DataGrid control is to display a single table of data from a dataset. However, the control can also be used to display multiple tables, including related tables. The display of the grid is adjusted automatically according to the data source.

What is the use of a grid control explain the properties and methods of a grid control?

Store all elements from this grid into a template form. It is usefull when user want to load more pages and items without flickering. If the value of one property is not entirely visible, you can activate tooltips with this property.

What is a grid record?

The most common form type or part of a form used to display records and collections of records is a grid, which displays a collection of records using a tabular (row-column) format. A grid can constitute the sole content of a form, in which case it is called a grid form.


1 Answers

I have a web app that also makes extensive use of various FindControl permutations in order to accomplish what you describe. Although it is brittle (don't change control IDs without testing), it can be made slightly less cumbersome through some utility functions. Here are all the FindControl-type functions I use -- this may at least assist you.

/// <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);
 }
}
like image 179
Jeremy Fuller Avatar answered Oct 13 '22 19:10

Jeremy Fuller