Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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
0
rmelancon1
Asked:
rmelancon1
  • 3
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now