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

  • Home
  • SEARCH
  • 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 171699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:02:03+00:00 2026-05-11T13:02:03+00:00

I have a TemplateField that is dynamically added to a custom GridView. void ITemplate.InstantiateIn(System.Web.UI.Control

  • 0

I have a TemplateField that is dynamically added to a custom GridView.

void ITemplate.InstantiateIn(System.Web.UI.Control container)     {         switch (_templateType)         {             case ListItemType.Header:                 if (this.ParentGridView.ShowDeleteHeaderImage)                 {                     Image hImg = new Image();                     hImg.ImageUrl = this.ParentGridView.DeleteHeaderImageUrl;                     hImg.AlternateText = 'Mark for Deletion';                     container.Controls.Add(hImg);                 }                 else                 {                     Label l = new Label();                     l.Text = 'Del';                     container.Controls.Add(l);                 }                 break;             case ListItemType.Item:                 container.Controls.Add(new CheckBox());                 break;             case ListItemType.EditItem:                 break;             case ListItemType.Footer:                 QLImageButton deleteButton = new QLImageButton();                 deleteButton.Settings.ImageId = 'cmdQLGVDelete';                 deleteButton.Settings.ImageUrl = this.ParentGridView.DeleteImageUrl;                 deleteButton.CommandName = 'Delete';                 container.Controls.Add(deleteButton);                 break;         }     } 

In response to a grid Command (insert/update/delete), a method called GetRowControls is called which iterates through the columns in the particular gridrow, and adds each of its controls to a Dictionary.

Dictionary<string, WebControl> GetRowControls(GridViewRow row) ...  rowControls.Add(ctrl.ID, (WebControl)ctrl);  ... 

So this works fine for both template field and bound controls added declaratively, as well as dynamic-non template fields added programatically.

However when the control is a TemplateField control added dynamically ctrl.ID is always null and therefore the statement above throws an exception.

I’ve looked into this with Reflector because I found that when I examined the variable in the immediate window in VS 2005 i.e. ?ctrl, ctrl.ID WOULD list a value. I’ve since established that this is because in listing ?ctrl in the immediate window, the proprty ClientID is called and ClientID calls EnsureId(), which in turn sets ID.

public virtual string ClientID {     get     {         this.EnsureID();         string uniqueID = this.UniqueID;         if ((uniqueID != null) && (uniqueID.IndexOf(this.IdSeparator) >= 0))         {             return uniqueID.Replace(this.IdSeparator, '_');         }         return uniqueID;     } } 

So I’m assuming that ClientID, UniqueId and ID are all null – although as above just reading the first two will trigger all to be set. Also note that NamingContainer is not null. It has been set.

So the work around for this is quite simple i.e. check for ctrl.ID==null and if so simply read ctrl.ClientID. And thats what I’ve done because time wise I’ve really got to get a wriggle on. But I’m still interested in the answer if anyone knows it off the top of their heads.

Why is the ID value of a child control, of a dynamically added TemplateField, set at a different time from that of other controls?

  • 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. 2026-05-11T13:02:03+00:00Added an answer on May 11, 2026 at 1:02 pm

    It is not that they behave differently, but that almost always when you add a control declaratively you set the ID right away. Try adding a label with no ID to a page and browse the control collection and check its ID, it will be null (make sure not to show its clientID since it would get the ID filled):

    <asp:Label runat='server'>something</asp:Label> 

    Also note that if you run it like that you get an span with no ID.

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

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can ensure intellisense by adding schema references to wss.xsd,… May 11, 2026 at 2:16 pm
  • added an answer Remove the data.Dispose(). The attachments are being added by reference… May 11, 2026 at 2:16 pm
  • added an answer With your version of jQuery, it works only with a… May 11, 2026 at 2:15 pm

Related Questions

I have a gridview that I have added a checkbox column using a templatefield
I have a TemplateField in a DetailsView and its input should be one of
I have a Gridview with ImageButtons added to a column via a templatefield. I've
I have a Gridview in which i have two templatefields of dropdownlist. I bound
I have a web-service that I will be deploying to dev, staging and production.
I searched for a few days now but didn't find a solution. So here
I have a asp.net page I'm writing and I'm perplexed by this problem. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.