Need ID assigned to TextBox in ItemTemplate of a DataGrid for javascript code

I need to add javascript to a button that checks the value of a textbox that is inside a datagrid.  The textbox is in an ItemTemplate within a datagrid.  The button is outside of the datagrid.  The problem is that asp.net generates unique ids for all of the textboxes within the grid at runtime.  So assuming I had 4 rows in my grid named "myDataGrid" and my textbox had an id of "txtName", at runtime in the browser the id would read "myDataGrid__ctl4_txtName".  So if I need javascipt in my button to check this control, how do I get the id that asp.net generates?  The name will change depending on how many rows are in the data grid so I can't hardcode it to that name.

Thanks,
Robb
rmelancon1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
if the instance of your textbox in your codebehind is textBox1, then it's clientID will be textBox1.ClientID.

It will always reflect the rigtt id once the control has been added to the page.
0
rmelancon1Author Commented:
Well, I tried that and it still gives me "txtName" without the "myDataGrid__ctl5_" part so then I get a javascipt error because txtName is undefined as in the html that is output it is now named myDataGrid__ctl5_txtName
0
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
ae you sure the textbox is added to the page 's control collection at the moment you're requesting this property?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

rmelancon1Author Commented:
Well, the textbox in question is in an item template of the datagrid so the only way I can see to get the reference to the actual textbox is on the _ItemCreated event of the grid then call e.Item.FindControl("txtName") store that in a textbox object then access the ID with textbox.ClientID.

So I'm not sure of the answer to your question.  Is there a way to get a reference to the textbox in the itemtemplate say in the prerender of the page?
0
teejay21Commented:
How are you binding your datagrid?  Try using the ItemDataBound event instead of the ItemCreated event (if this applies to you).

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rmelancon1Author Commented:
Well, that did it.  I moved the code from the ItemCreated to the ItemDataBound and now the ClientID has the correct information.

Thanks to all who responded.

Robb
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.