开发者

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

开发者 https://www.devze.com 2023-03-29 13:20 出处:网络
I have some text boxes in my page. I want to get all text box values in开发者_JS百科 an array and insert in to table of database.

I have some text boxes in my page. I want to get all text box values in开发者_JS百科 an array and insert in to table of database.

Is there any option to work by loop


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


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


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
            }
       }
   } 


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); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消