0

If I dynamically create a control in code-behind, how would I bind jQuery stuff to that control?

IE (this doesnt work, obviously) mycontrol.Extend(".animate()");

Thanks!

* LET ME CLARIFY --- I want to bind these properties to this control FROM THE CODE BEHIND... so

Todd Vance
  • 4,627
  • 7
  • 46
  • 66
  • You could have a script on client-side using live method. It will work with controls create dynamically on the page. See it http://api.jquery.com/live/ – Felipe Oriani Jun 05 '12 at 17:37
  • Server-side controls are created and rendered on page load. Don't need to use the `live()` methods unless something is being changed *after* the DOM is generated. – Terry Jun 05 '12 at 17:40

3 Answers3

1

If this is for WebForms, then controls expose their dynamically created ID with .ClientID. Using that, you could have something like this...

var emailId = '#<%= EmailInput.ClientID %>';

$(function() {
    $(emailId) // ...
});
T. Stone
  • 19,209
  • 15
  • 69
  • 97
0

You have to find out the client-side ID of the control (the ID it gets rendered with).
Depending on what version of ASP.NET you are using this can change.

If you use a DOM inspector you can find this information and then you can use the element as normal.

// Check if DOM is ready (eg. everything has been rendered)
$(function() {
   // Do stuff with the elements
   $('#myControlsClientID').animate();
});
Terry
  • 14,099
  • 9
  • 56
  • 84
0

one simple way is to assign CSSClass properties to the dynamically created controls and target these classes using jQuery

AHMED EL-HAROUNY
  • 836
  • 6
  • 17