Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:38:35+00:00 2026-06-01T14:38:35+00:00

Hi i added template field dynamically to gridview by implementing ITemplate interface. The template

  • 0

Hi i added template field dynamically to gridview by implementing ITemplate interface.
The template field contains some controls like label and textboxes. how do i get these controls in row databound event.

I am not able to get when i do gridviewrow.findcontrol(“id”) as i do normally when we add templatefield from aspx page.

The way i added template field is like this

public class CustomGridViewColumn : ITemplate
{
ListItemType _liType;
string _columnName;

        public CustomGridViewColumn(ListItemType type, string column)
        {
            _liType = type;
            _columnName = column;

        }
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {
            switch (_liType)
            {
                case ListItemType.Header:
                    Label lblHeader = new Label();
                    lblHeader.Text = _columnName;
                    container.Controls.Add(lblHeader);
                    break;
                case ListItemType.Item:
                    Label lblItem = new Label();
                    lblItem.DataBinding += new EventHandler(lbl_DataBinding);
                    lblItem.ID = "lbl" + _columnName;
                    lblItem.ClientIDMode = ClientIDMode.Predictable;
                    container.Controls.Add(lblItem);

                    DropDownList ddl = new DropDownList();

                    ddl.DataBinding += new EventHandler(ddl_DataBinding);
                    ddl.ID = "ddl" + _columnName;
                    ddl.Visible = false;
                    container.Controls.Add(ddl);
                    break;


            }
        }

}

Now i want access the label and dropdown which i have added using this code.
when i do gridviewrow.findcontrol(“id”) i am not getting them.
Can any one please help me.

I am geeting when i go through all the rows and try to find but
i have a check box in a row when i select it all labels should diappear and ddls dhould appear
for this i am using the follwoing code.

protected void chkEdit_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkEditTest = (CheckBox)sender;
GridViewRow grow = (GridViewRow)chkEditTest.NamingContainer;
DropDownList ddl = (DropDownList)grow.FindControl("ddl");
Label lbl= (Label)grow.FindControl("lbl");
}

when i do this i am not able to get the controls.
it seems like controls are disapppearing on postback..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:38:37+00:00Added an answer on June 1, 2026 at 2:38 pm

    This is what I came up with and I can able to get the control reference in the code behind.

        public class CustomGridViewColumn : ITemplate
        {
        ListItemType _liType; string _columnName;
    
        public CustomGridViewColumn(ListItemType type, string column)
        {
            _liType = type;
            _columnName = column;
    
        }
    
        void ITemplate.InstantiateIn(Control container)
        {
            switch (_liType)
            {
                case ListItemType.Header:
                    Label lblHeader = new Label();
                    lblHeader.Text = _columnName;
                    container.Controls.Add(lblHeader);
                    break;
                case ListItemType.Item:
                    Label lblItem = new Label();
                    lblItem.DataBinding += new EventHandler(lblItem_DataBinding);
                    lblItem.ID = "lbl" + _columnName;
                    lblItem.ClientIDMode = ClientIDMode.Predictable;
                    container.Controls.Add(lblItem);
    
                    DropDownList ddl = new DropDownList();
    
                    ddl.DataBinding += new EventHandler(ddl_DataBinding);
                    ddl.ID = "ddl" + _columnName;
                    ddl.Visible = false;
                    ddl.DataSource = new string[] { "Hello", "World" };
                    container.Controls.Add(ddl);
                    break;
    
    
            }
        }
    
        void ddl_DataBinding(object sender, EventArgs e)
        {
    
        }
    
        void lblItem_DataBinding(object sender, EventArgs e)
        {
    
        }
    
    }
    
     protected void Page_Load(object sender, EventArgs e)
     {
    
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        DataRow oItem = dt.NewRow();
        oItem[0] = "Deepu";
        dt.Rows.Add(oItem);
        oItem = dt.NewRow();
        oItem[0] = "MI";
        dt.Rows.Add(oItem);
        GridView gv = new GridView();
        gv.ID = "myGridView";
        gv.AutoGenerateColumns = false;
        BoundField nameColumn = new BoundField();
        nameColumn.DataField = "Name";
        nameColumn.HeaderText = "Name";
        gv.Columns.Add(nameColumn);
    
        TemplateField TmpCol = new TemplateField();
        TmpCol.HeaderText = "Template Column";
        gv.Columns.Add(TmpCol);
        TmpCol.ItemTemplate = new CustomGridViewColumn(ListItemType.Item, "TEST");
        gv.DataSource = dt;
        gv.DataBind();
        Form.Controls.Add(gv);
    
    
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView gv = Form.FindControl("myGridView") as GridView;
        foreach (GridViewRow item in gv.Rows)
        {
            var ddl = item.FindControl("ddlTest") as DropDownList;
            if (ddl != null)
            {
                ddl.Visible = true;
            }
    
            var lbl = item.FindControl("lbl") as Label;
            if (lbl != null)
            {
                lbl.Text = "hello";
            }
        }
    }
    
    <form id="form1" runat="server">
    
    
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    
    </form>
    

    Thanks

    Deepu

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added an image button in the template field of the gridview. The
I have a TemplateField that is dynamically added to a custom GridView. void ITemplate.InstantiateIn(System.Web.UI.Control
I have a gridview that displays items details, I added two template fields one
I have a GridView which has following template field that holds the filename, when
My GridView contains 20 columns which are added programmatically (DataTable, DataColumn, DataRow and DataSet).
I have added a field Reported By to the CMMI Template Bug work item
I have a Gridview with ImageButtons added to a column via a templatefield. I've
I have a gridview that I have added a checkbox column using a templatefield
When a template is added using the add-template stsadm command, it becomes available to
I created a tab bar application from the template and added a navigation controller

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.