Retrieve dynamically generated TextBox value inside ASP.Net GridView Template field

Last Reply on Apr 27, 2018 03:44 AM By kalpesh

Posted on Apr 24, 2018 02:20 AM

Hi All,

I want to change value from textbox and then update value. So I must use rowupdating event but i cannot find textbox

I use code below

TemplateField field = new TemplateField();
field.HeaderText = j.employee_name;
field.ItemTemplate = new TemplateGenerator(ListItemType.Item, j.employee_name, i);
field.EditItemTemplate = new TemplateGenerator(ListItemType.EditItem, j.employee_name, i);
GridView1.Columns.Add(field);

 

    public class TemplateGenerator : ITemplate // Class inheriting ITemplate
    {
        ListItemType type;
        string columnName;
        int index;
        public TemplateGenerator(ListItemType t, string cN,int id)
        {
            type = t;
            columnName = cN;
            index = id;
        }
        // Override InstantiateIn() method
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (type)
            {
                case ListItemType.EditItem:
                    TextBox txt = new TextBox();
                    txt.ID = "txtmark" + index;
                    txt.Width = Unit.Pixel(30);
                    txt.DataBinding += new EventHandler(txt_DataBinding);
                    container.Controls.Add(txt);
                    break;
                case ListItemType.Item:
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(lbl_DataBinding);
                    container.Controls.Add(lbl);
                    break;
            }
        }

        private void lbl_DataBinding(object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            GridViewRow container = (GridViewRow)lbl.NamingContainer;
            object bindValue = DataBinder.Eval(container.DataItem, columnName);
            // Adding check in case Column allows null values
            if (bindValue != DBNull.Value)
            {
                lbl.Text = bindValue.ToString();
            }
        }

        private void txt_DataBinding(object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            GridViewRow container = (GridViewRow)txt.NamingContainer;
            object bindValue = DataBinder.Eval(container.DataItem, columnName);
            // Adding check in case Column allows null values
            if (bindValue != DBNull.Value)
            {
                txt.Text = bindValue.ToString();
            }
        }
    }

and then i use rowupdating event

TextBox txt = GridView1.Rows[e.RowIndex].FindControl("txtmark1") as TextBox;

but textbox is null

how to get value from textbox

Thank you

Results 1 - 5 of 6 12
Posted on Apr 24, 2018 02:20 AM
Hi @ptdanh392,
Please try the following

Retrieve (Get) readonly TextBox value inside ASP.Net GridView TemplateField using C#

It might help you.

Cheers Andrea.

Posted on Apr 24, 2018 02:48 AM

Thanks Andrea,

I want to change value from textbox and then update value. So I must use rowupdating event but i cannot find textbox


Posted on Apr 24, 2018 05:00 AM Modified on on Apr 24, 2018 05:00 AM

You need to retain the dynamic textboxes on Page_PreInit event.

 C#

    protected void Page_PreInit(object sender, EventArgs e)
    {
        List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtmark")).ToList();
        int i = 1;
        foreach (string key in keys)
        {
            // call your Add TemplateField code            
            i++;
        }
    }

 

 


Posted on Apr 26, 2018 03:29 AM

I use Page_PreInit event, but I cannot add templatefield into gridview cause gridview is null.

Please help me


Posted on Apr 27, 2018 01:43 AM Modified on on Apr 27, 2018 01:58 AM

I dont use Page_PreInit event, but i use Init event of gridview to solve this problem