C# looping through controls problem

DB_Fury
DB_Fury used Ask the Experts™
on
I am looping through controls in C# and the following code will work when ever there is a textbox, but does anyone know what i can write incase the next control is a checkbox?
for (int i = 1; i <= strArray.Length; i++)
        {
   
            TextBox txt =  (TextBox)row.Cells[i].Controls[0];  
    
            Update = Update + " " + strArray[i].ToString() + "=" + txt.Text + ",";
         
            txt.Dispose();
            
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
It depends what are You trying to do.
But try something similar to

                string s;
                if (Controls[i] is TextBox)
                    s = (Controls[i] as TextBox).Text;
                if (Controls[i] is CheckBox)
                    s = (Controls[i] as CheckBox).Text;

Author

Commented:
thanks for the reply mokule, that didnt work for me, but by playing around i obtained the following that works:

string Control_type = row.Cells[i].Controls[0].GetType().ToString();

if (Control_type == "System.Web.UI.WebControls.CheckBox")
...
else if (Control_type == "System.Web.UI.WebControls.TextBox")
I know this is closed, and I don\t expect any points. I once needed to find a way to loop through all of a forms controls and do stuff to them. the following is code I used that can easyily be followed and modified to fit your needs.
private void LoopThroughAndValidateControls(Control.ControlCollection formControls, ref bool alertUser)
        {
            #region loop through controls

            foreach (Control controls in formControls)
            {
                if (controls is ComboBox)
                {
                    if ((controls as ComboBox).Enabled == true)
                        (controls as ComboBox).BackColor = validator.CheckForBlankValue((controls as ComboBox).Text, ref alertUser);
                }
                if (controls is TextBox)
                {
                    if ((controls as TextBox).Enabled == true && (controls as TextBox).Tag != "IGNORE" && (controls as TextBox).Tag != null)
                        (controls as TextBox).BackColor = validator.ValidateTextBox((controls as TextBox).Text, (controls as TextBox).Tag.ToString(), ref alertUser);
                }
                if (controls.HasChildren)
                    LoopThroughAndValidateControls(controls.Controls, ref alertUser);
            }
            #endregion
        }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial