0

I'm using asp.net/vb.net 4.0, visual studio 2010, and the linq-to-entities model with dynamic data scaffolding to give users generated forms to edit data in a SQL database.

I have added some buttons that call javascript functions to the insert.aspx in the PageTemplates folder.

At first I was using something like

 var account = document.getElementById("ctl00_ContentPlaceHolder1_FormView1_ctl04_ctl00___account_TextBox1");

to access the elements by whatever random ID asp.net chose to generate.

The problem is that whenever a new data field is added to the SQL db, asp.net changes the random IDs for all the other elements to fit the new one in. I would resolve this as per this question; but because the fields are generated, there are no asp.net objects to call .ClientID on.

Is there a solution to this?

Community
  • 1
  • 1
DylanReile
  • 756
  • 7
  • 11

2 Answers2

0

I ended up using document.querySelector. It is much slower, but solves the problem. The following code will store the first element containing "account".

var test = document.querySelector("[id*=account]");
DylanReile
  • 756
  • 7
  • 11
0

You can override the default naming convention by setting the ClientId property for each of the controls that you need to reference in your JS code. That will make sure that they're always the same because they're not generated at compile time.

Dave Mroz
  • 1,509
  • 2
  • 14
  • 27
  • Yes, but the controls are generated dynamically so there's no where to set that property. – DylanReile Jan 15 '14 at 21:58
  • It can still be done, but you'll probably have to override the method that generates the input which sounds like a headache. Is this JS so that you can attach an event back to the input box that's autogenerated, or is it for something else? – Dave Mroz Jan 15 '14 at 22:10
  • The JS is for autofilling some fields based on user input. There's an html button that calls it. I've yet to see an example of overriding the generating input method that was in vb.net instead of C#. Do you have one? – DylanReile Jan 15 '14 at 22:22