Unable to Find Control in RowEditing event for TemplateFields in GridView

1.3k views Asked by At

I am adding columns to gridview from codebehind. For boundfields, i am able to find control and textbox value while updating the row. But for template fields, i am not able to get controls to the code behind, so i am unable to get the textbox value. Can please suggest how to get textbox value in codebehind for template fields

My Code

protected void gvbind()
{
    conn.Open();
    string Query = "SELECT * FROM testactiondatabase_db.actions";
    MySqlCommand MyCommand2 = new MySqlCommand(Query, conn);

    MySqlDataAdapter MyAdapter = new MySqlDataAdapter();
    MyAdapter.SelectCommand = MyCommand2;
    DataTable dt = new DataTable();
    MyAdapter.Fill(dt);

    TaskGridView.Columns.Clear();
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        if (dt.Columns[i].ColumnName.ToString().ToUpper().Contains("DATE"))
        {
            TemplateField bfield = new TemplateField();
            bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, dt.Columns[i].ColumnName.ToString());
            bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, dt.Columns[i].ColumnName.ToString());
            bfield.EditItemTemplate = new GridViewTemplate(ListItemType.EditItem, dt.Columns[i].ColumnName.ToString());
            TaskGridView.Columns.Add(bfield);
        }
        else
        {
            BoundField boundfield = new BoundField();
            boundfield.DataField = dt.Columns[i].ColumnName.ToString();
            boundfield.HeaderText = dt.Columns[i].ColumnName.ToString();
            TaskGridView.Columns.Add(boundfield);
        }
    }

    TaskGridView.DataSource = dt;
    TaskGridView.DataBind();
    TaskGridView.Width = 600;
    TaskGridView.HeaderStyle.CssClass = "header";
    TaskGridView.RowStyle.CssClass = "rowstyle";

    conn.Close();
}

public class GridViewTemplate : ITemplate
{
    ListItemType _templateType;

    string _columnName;

    public GridViewTemplate(ListItemType type, string colname)
    {

        _templateType = type;

        _columnName = colname;
    }

    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (_templateType)
        {
            case ListItemType.Header:
                Label lbl = new Label();
                lbl.Text = _columnName;
                container.Controls.Add(lbl);
                break;

            case ListItemType.Item:
                Label lb1 = new Label();
                lb1.DataBinding += new EventHandler(lb1_DataBinding);
                container.Controls.Add(lb1);
                break;

            case ListItemType.EditItem:
                TextBox tb1 = new TextBox();
                tb1.ID = _columnName;
                tb1.DataBinding += new EventHandler(tb1_DataBinding);
                tb1.Attributes.Add("class", "myDatePickerClass");
                container.Controls.Add(tb1);
                break;

            case ListItemType.Footer:
                CheckBox chkColumn = new CheckBox();
                chkColumn.ID = "Chk" + _columnName;
                container.Controls.Add(chkColumn);
                break;
        }
    }

    void tb1_DataBinding(object sender, EventArgs e)
    {
        TextBox txtdata = (TextBox)sender;
        GridViewRow container = (GridViewRow)txtdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, _columnName);
        if (dataValue != DBNull.Value)
        {
            txtdata.Text = Convert.ToDateTime(dataValue.ToString()).ToString("dd/MM/yyyy");
        }
    }

    void lb1_DataBinding(object sender, EventArgs e)
    {
        Label txtdata = (Label)sender;
        GridViewRow container = (GridViewRow)txtdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, _columnName);
        if (dataValue != DBNull.Value)
        {
            txtdata.Text = Convert.ToDateTime(dataValue.ToString()).ToString("dd/MM/yyyy");
        }
    }
}

I am using the following code for reading the template field textbox value while updating the row but the controls are 0 for the cell.

GridViewRow row = (GridViewRow)TaskGridView.Rows[e.RowIndex];
TextBox textnew = (TextBox)row.Cells[j].Controls[0];
1

There are 1 answers

1
VDWWD On

Try using FindControl.

TextBox textnew = TaskGridView.Rows[e.RowIndex].FindControl(_columnName) as TextBox;

But if I use your snippet like this, with setting an EditIndex manually, it also works... The value of te TextBox is Cell 1 is changed.

TaskGridView.EditIndex = 1;
TaskGridView.DataSource = dt;
TaskGridView.DataBind();
TaskGridView.Width = 600;
TaskGridView.HeaderStyle.CssClass = "header";
TaskGridView.RowStyle.CssClass = "rowstyle";

GridViewRow row = (GridViewRow)TaskGridView.Rows[TaskGridView.EditIndex];
TextBox textnew = (TextBox)row.Cells[1].Controls[0];
textnew.Text = "test";