Results 1 to 9 of 9

Thread: Iterating through form controls

  1. #1

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80

    Iterating through form controls

    Why can't I see my textboxes when iterating through my WebForm??

    This code is only picking up HtmlForm and a couple of Literalcontrols.
    VB Code:
    1. foreach (Control t in Page.Controls)
    2. {
    3.     if (t is TextBox)
    4.     {
    5.         t.DataBind();
    6.     }
    7. }

    I've also tried using 'this.Controls' !!
    You may follow, but not to close!

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You have to use recursion:

    private void Page_Load .....
    {
    .....
    BindTextboxes(Page);
    }


    private void BindTextboxes(Control aControl)
    {
    foreach (Control t in aControl.Controls)
    {
    if (t is TextBox)
    {
    t.DataBind();
    }
    else
    BindTextboxes(t);
    }
    }

  3. #3

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    Thanks but I'm not sure I understand how this is going to help uncover my textboxes by iterating through my page??

    Also, this code resides within a dropdownlist__SelectedIndexChanged event. Does that make a difference?
    You may follow, but not to close!

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I wrote it up without running it, so it may be a little off still, but the concept will allow you to find controls of textbox type.

    What happens is those three literal controls that you seen it loop through have child controls in a collection that needs to be looked at as well. By doing it recursively, we can check all the child controls of each control and doing the binding when we find a textbox.

  5. #5

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    Well, I've written this..
    VB Code:
    1. foreach (Control t in this.Controls)
    2. {
    3.     if (t is TextBox)
    4.     {
    5.         t.DataBind();
    6.     }
    7.     else
    8.     {
    9.         if (t.HasControls())
    10.         {
    11.             foreach(Control ct in t.Controls)
    12.             {
    13.                     if (ct is TextBox)
    14.                 {
    15.                     ct.DataBind();
    16.                 }
    17.             }
    18.         }
    19.     }
    20.                
    21. }

    ..and it finds the child controls but they return as a LiteralControl with the names of my textboxes. So I have found my textboxes but why are they coming back as LiteralControls?? I can't test against the names b/c there's to many. What can I do to reveal the true textbox?

    Thanks!!!
    You may follow, but not to close!

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    OK, here is code that works, adapt it to your cause:

    Here is how I call the method to find a textbox control by name on the page:
    Code:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    	TextBox tx = FindTextBoxControl("TextBox1", this);
    
    	if(tx != null)
    		Response.Write(tx.Text);
    	else
    		Response.Write("NotFound");
    }
    And here is the method that finds the control.
    Code:
    private TextBox FindTextBoxControl(string textName, Control theControl)
    {
    	foreach (Control t in theControl.Controls)
    	{
    		if (t is TextBox)
    		{
    			if(((TextBox)t).ID == textName)
    				return (TextBox)t;
    		}
    		else
    		{
    			if (t.HasControls())
    				return FindTextBoxControl(textName, t); 
    		}			
    	}
    	return null;
    }
    The reason they are nested are because a lot of container controls are literal controls. Literal controls contain child controls. You can actually nest container controls to be very deep. Like a table cell control can hold many textbox, label controls, etc...

    You have to recurse the control tree to find what you are looking for.

  7. #7

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    Thanks for the help, however, I don't need to find a particular textbox using a distinct name. I just want a way to iterate through all controls on the page, if it's a textbox then bind it. I have 30+ textboxes on my form, so calling a textbox by name is to cumbersome. I just need to identify the control as a textbox and then bind it.
    You may follow, but not to close!

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Wow, I gave you all the code to do what you want, you just have to tweak it a bit. I don't think you even want to put the effort into understanding why I am doing it this way. Since you want it all written out for you, call this method when you want to bind all the textboxes passing in the page object to it:
    Code:
    private void BindTextBoxControls(Control theControl)
    {
    	foreach (Control t in theControl.Controls)
    	{
    		if (t is TextBox)
    		{
    			t.DataBind();
    		}
    		else
    		{
    			if (t.HasControls())
    				BindTextBoxControls(t); 
    		}			
    	}
    }

  9. #9

    Thread Starter
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    I do understand your point with "recursive" engineering!! Although I'm not using recursive programming I am able to iterate through all my controls now. Even though it's finding my textboxes it's not "seeing" them as textbox controls, therefore it skips the binding process.

    Try it yourself and surprise me!!
    You may follow, but not to close!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width