Setting a 'default' value to controls (textbox) (loop controls) RRS feed

  • Question

  • User1729086557 posted

    I have aspx form with several textboxes.They are populated with values from a table via retrieval of a dataview.

    If the row count for the dataview is 0, then I'd like to reset the values in all textboxes. How can I loop through all the textboxes and set a default value?

    I've tried the following, but no sucess.

    For Each ctrl As Control In Page.Form.Controls
        If TypeOf ctrl Is WebControls.TextBox Then
           CType(ctrl, WebControls.TextBox).Text = ""
        End If

    My first For Loop was using Page.Controls but it returned only 1 count.

    The textboxes are inside a tabpanel (ajax), so do I have to locate the tabpage and then find all it's controls (for each tabpage)?

    Wednesday, November 11, 2009 4:01 PM


  • User-1360095595 posted

    I think you're going to have to use some form of a recursive method call, that will iterate through all the controls in the tabpanel in question.  Something like this:

    private void ClearTextBoxes(ControlCollection cc)
    	foreach (Control control in cc)
    		if (control.HasControls())
    		if (control is TextBox)
    			(control as TextBox).Text = "";


    You would call this passing in maybe TabPanel1.Controls.  Not a VB person, so try the link in my signature to translate.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2009 8:45 PM