代码改变世界

FindControl

2010-08-26 15:37  Tracy.  阅读(360)  评论(0编辑  收藏  举报

Here is some C# code which should be easy to convert. Pass in the id "lblRecTonT" and the control collection Page.controls or GridView1.Controls. This is guaranteed to work if the ID exists.
public static System.Web.UI.Control FindControl(string controlId, System.Web.UI.ControlCollection controls)
            {
                  for (int counter = 0; counter < controls.Count; ++counter) // loop through each control in the control collection
                  {
                        if (controls[counter].ID == controlId) //see if the current control is a match
                        {
                              return controls[counter];
                        }
                        else if (controls[counter].Controls.Count > 0) // check the child controls
                        {
System.Web.UI.Control FoundControl = FindControl(controlId, controls[counter].Controls); // recursive call to check child controls
                              if (FoundControl != null) // control found in child's controls
                              {
                                    return FoundControl;
                              }
                        }
                  }
                  return null;
            }