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

Posted on 2005-03-05
Medium Priority
Last Modified: 2008-03-17
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.

Question by:rmelancon1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13467169
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.

Author Comment

ID: 13467239
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
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13467303
ae you sure the textbox is added to the page 's control collection at the moment you're requesting this property?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13468870
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?

Accepted Solution

teejay21 earned 500 total points
ID: 13469121
How are you binding your datagrid?  Try using the ItemDataBound event instead of the ItemCreated event (if this applies to you).


Author Comment

ID: 13470553
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.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question