Home » ASP.NET » Access a control by ID From Within a Databound Control

Access a control by ID From Within a Databound Control


Databound controls are at once very easy and very frustrating.  If you just need to do some simple databinding that gets a list of items on the screen and you need the ability to edit those items, you are all set.

But once you need to do anything that deviates from that simple pattern, you are kind of stuck.  One of the problems involves accessing a control in a templated item.

Probably the most common place this shows up is in the FormView control.  But you’ll also experience the problem using the GridView control with a templated column.

There is a pretty simple solution to the problem.  If you are in a form view, all you need to do is use the FindControl() method of any control.  The problem is finding which parent control houses the control we are looking for.  So what we really need is a recursive control that looks for the control with a particular ID in any of the controls that are a child of the control or any of its child controls or any of its child controls, etc., until it finds the control.

I’ve written such a function for my utility box.

public static Control FindControlByID

(string id, ControlCollection controls)


Control returnControl = null;

foreach (Control c in controls)


if (c.ID == id)

return c;

returnControl = FindControlByID(id, c.Controls);

if (returnControl != null)

return returnControl;


return returnControl;


Notice, it is a static method so we can call it from anywhere without having to instantiate the class it is in.

So now, if I’m in a row and I need to find another control in that row, I can write code like this

Protected Sub m_grid_RowUpdating

(Object sender,GridViewUpdateEventArgs e)


int v= (DropDownList)(MyUtils.FindControlByID

(“m_dropDownListId”, (GridView)(sender)



The only real tricky point left is that you may need to climb up to a parent to grab the right controls collection.  But once you figure how how far up you need to climb, the rest is academic.

And before someone says it in the comments, obviously you’ll want to check to make sure you got the control using a if(var == null) statement prior to retrieving the value.

Other places talking about Finding Controls in ASP.NET

gridview edit – FindControl(“txtFirstName”); TextBox tLastName = (TextBox)gr.FindControl(“txtLastName”); TextBox tEmail = (TextBox)gr.FindControl(“txtEmail”); Label lFirstName = (Label)gr.FindControl(“lblFirstName”); Label lLastName = (Label)gr. …

Using FindControl for a control in the ItemTemplate of a ListView … – I’ve been playing around with the ListView control recently and am quite impressed with it. I like how it gives full control over the markup used as apposed.


Other post in ASP.NET

Related Post

  • EXT DataBound CheckboxGroupEXT DataBound CheckboxGroup Today I’ve decided to post a control I created for EXTjs that might be useful to you.  The problem I was running into was that I had a group of checkbox controls that all needed to be set […]
  • FormView Edit and AddFormView Edit and Add The FormView generally makes editing a record at a time pretty easy.  However, one of the biggest problems I've seen with this control is that there is no way of telling the FormView to […]
  • ASP.NET GridView Edit All Rows At OnceASP.NET GridView Edit All Rows At Once I just saw a question about this yesterday and realized that while I know how to do this, not everyone does.  So, here we go... Here's the problem.  You want to be able to edit […]
  • Multi-Step Processing in ASP.NETMulti-Step Processing in ASP.NET I received the following question a few days ago but I’ve been so busy with billable work that I just haven’t had a chance to answer it until now.  Actually, I’m still busy, but I […]
  • ListView and the DataPagerListView and the DataPager One of the new controls that Visual Studio 2008 brings us is the ListView.  Now, the ListView control is really several different controls all rolled into one.  There is a lot to see with […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS.Does your team need additional help in any of the above? Contact Dave today.