Iterating through TextBoxes in asp.net - why is this not working?

后端 未结 3 911
野趣味
野趣味 2021-01-05 10:12

I have 2 methods I tried to iterate through all my textboxes in an asp.net page. The first is working, but the second one is not returning anything. Could someone please exp

3条回答
  •  难免孤独
    2021-01-05 11:05

    you can try this piece of code to get list of all TextBoxes

    public partial class _Default : System.Web.UI.Page
       {
    
           public List ListOfTextBoxes = new List();
           protected void Page_Load(object sender, EventArgs e)
           {
               // after execution this line
               FindTextBoxes(Page, ListOfTextBoxes);
               //ListOfTextBoxes will be populated with all text boxes with in the page.
    
           }
    
    
           private void FindTextBoxes(Control Parent, List ListOfTextBoxes)
           {
               foreach (Control c in Parent.Controls) {
                   // if c is a parent control like panel
                   if (c.HasControls())
                   {
                       // search all control inside the panel
                       FindTextBoxes(c, ListOfTextBoxes);
                   }
                   else {
                       if (c is TextBox)
                       {
                           // if c is type of textbox then put it into the list
                           ListOfTextBoxes.Add(c as TextBox);
                       }
                   }
               }
           }
       }
    

提交回复
热议问题