Mohsin
Mohsin

Reputation: 902

retrieving the value of dynamically generated Textboxes in asp.net

i have a column in db which contain multiple values separated by comma now i want to edit it so i retrieve the values from db separate it and store it in string array then generate textboxes and assign the values to textboxes now i want to retrieve the updated values from that generated textboxes here is the code

    static string[] temp;
    static string[] temp1;
    static TextBox tbin;
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                barcode_lab.Text = GridView1.SelectedRow.Cells[1].Text;
                date_lab.Text = GridView1.SelectedRow.Cells[2].Text;
                string tin = GridView1.SelectedRow.Cells[3].Text;
                string tout = GridView1.SelectedRow.Cells[4].Text;

                //////////////conversion/////////////////////

                temp = tin.Split(',');
                for (int i = 0; i < temp.Length; i++)
                {
                     tbin = new TextBox();
                    tbin.Text = temp[i];
                    tbin.ID = "timein"+i;
                    PlaceHolder6.Controls.Add(tbin);
                    PlaceHolder6.Controls.Add(new LiteralControl("<br />"));
                }
          }

UPDATED:

protected void update_btn_Click(object sender, EventArgs e)
    {

            foreach (GridViewRow row in GridView1.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    List<TextBox> textBoxes = MissingExtention.GetAllControls(cell).Where(c => c is TextBox);

                }
            }
    }

public static class MissingExtention
{
    public static List<Control> FlattenChildren(this Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c).Where(a => a is TextBox)).Concat(children).ToList();
    }

    public static List<Control> GetAllControls(Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c)).Concat(children).ToList();
    }
}

now the following error occurs:

  1. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.GridView' because 'System.Web.UI.WebControls.GridView' does not contain a public definition for 'GetEnumerator'
  2. The best overloaded method match for 'GPServices.MissingExtention.GetAllControls(System.Web.UI.Control)' has some invalid arguments
  3. Argument 1: cannot convert from 'System.Web.UI.ControlCollection' to 'System.Web.UI.Control'

Upvotes: 2

Views: 1261

Answers (2)

Mohsin
Mohsin

Reputation: 902

ok got the most easiest way to do that

for (int i = 0; i < temp.Length; i++)
     {
         PlaceHolder6.Controls.Add(new LiteralControl("<input id='txt' name='txtName' type='text' value='"+temp[i]+"' />"));
         PlaceHolder6.Controls.Add(new LiteralControl("<br />"));
     }

protected void update_btn_Click(object sender, EventArgs e)
    {
        Label1.Text = Request.Form["txtName"];
    }

it also return me all the values of textboxes in single string which also helps me

Upvotes: 1

Nițu Alexandru
Nițu Alexandru

Reputation: 714

Try this:

New Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for OhterMethods
/// </summary>
public static class OhterMethods
{
    public static List<Control> FlattenChildren(this Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c).Where(a => a is TextBox ||     a is Label || a is Literal || a is Button || a is GridView || a is HyperLink || a     is DropDownList)).Concat(children).ToList();
    }

    public static List<Control> GetAllControls(Control control)
    {
        var children = control.Controls.Cast<Control>();
        return children.SelectMany(c => FlattenChildren(c)).Concat(children).ToList();
    }
}

Get all textboxes

foreach (GridViewRow row in GridView1.Rows)
{
    foreach (TableCell cell in row.Cells)
    {
        List<TextBox> textBoxes = OtherMethods.GetAllControls(cell).Where(c => c is TextBox);
    }
}

You can also get a specific textbox by id:

OtherMethods.GetAllControls(cell).FirstOrDefault(c => c is TextBox && c.ID == "txtTextBox")

You have to get each row of the gridview. After that, for a specific column or for each one of them, get all text boxes and extract the value. You have to call the above method with the column as parameter. If you have to update a specific row by id, you can add an attribute to each textbox or add a hidden field to a column of each row and extract the value.

Upvotes: 0

Related Questions