find control in page

后端 未结 9 1075
孤城傲影
孤城傲影 2020-12-11 02:37

HTML


    
9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 03:40

    This should find any Control on page

    private Control FindALL(ControlCollection page, string id)
    {
      foreach (Control c in page)
      {
        if (c.ID == id)
        {
          return c;
        }
    
        if (c.HasControls())
        {
          var res = FindALL(c.Controls, id);
    
          if (res != null)
          {
            return res;
          }
        }     
      }
      return null;
    }
    

    Call like:

    Button btn = (Button)FindALL(this.Page.Controls, "a");
    btn.Text = "whatever";
    

提交回复
热议问题