Binding a jQuery event to code created in ASPX code-behind?
For reasons out of my control I have both client-side JavaScript that builds up a row of an HTML table and similar code in the code-behind page that triggers after a button event. On the client-side, there is an extra method that is called after the rows are created that unobtrusively and dynamically binds several live
events to each input in the new row.
I need to get that same functionality from the row(s) created in the code-behind event handler. I tried simply calling the same method via ClientScript.RegisterClientScriptBlock
and ClientScript.RegisterStartupScript
but neither of them worked and no events trigger on the code-behind row (but they function as expec开发者_StackOverflowted if I then add a client-side row).
Any idea how to handle this? Should I call the client-side binding code elsewhere on the page rather than in the client-side creation method, so it will trigger on the code-behind? Or is there another way to call it from the code-behind?
Create a startup script that parses the rows in bulk during page initialization as in:
$("table").find("tr").live(..);
So from the server-side, you need to render a script that processes the table in bulk, and then manage the client-side interactions in a singular fashion.
You should be able to render something out like this; please post some code if you can't.
HTH.
If you use live
and the rows all share a common class then it shouldn't matter how they are added, the event should apply to them. Simply run it once on page load and that's it.
精彩评论