Vishnu Singh Parihar
Vishnu Singh Parihar

Reputation: 11

how to get value of more than one text box in a loop

I have some text boxes in my page. I want to get all text box values in an array and insert in to table of database.

Is there any option to work by loop

Upvotes: 1

Views: 1411

Answers (4)

artwl
artwl

Reputation: 3582

private void FindSelecedControl(Control control) 
{
    if (control is TextBox)
    {
        TextBox txt = (TextBox)control;
        txt.Enabled = false;
    }
    else
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            FindSelecedControl(control.Controls[i]);
        }
    } 
}

foreach (Control control1 in this.Form.Controls) 
{
     FindSelecedControl(control1); 
}

Upvotes: 1

Paolo Falabella
Paolo Falabella

Reputation: 25874

public IEnumerable<string> AllTextsFromTextboxes()
{
    foreach (var control in Page.Controls.OfType<TextBox>())
    {
        yield return control.Text;    
    }
}

Upvotes: 11

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

yes, you can put your controls in panel and then iterate and get value. e.g.

foreach (Control ctrl in Panel1.Controls)
    {
        if (ctrl.GetType().Name == "TextBox")
        {
            if (((TextBox)ctrl).Text != string.Empty)
            {
                // do stuff here
            }
       }
   } 

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

you can try something on these lines, if all the textbox are on the page directly

foreach(Control c in Page.Controls)
{
    if (c is TextBox)
    {
        //get the text
    }
}

This will not work for child controls for that you will have to recursively iterate

Upvotes: 1

Related Questions