2

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'

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.