将 jQuery 事件绑定到 ASPX 代码隐藏中创建的代码?
由于我无法控制的原因,我同时拥有构建一行 HTML 表格的客户端 JavaScript 和在按钮事件后触发的代码隐藏页面中的类似代码。在客户端,创建行后会调用一个额外的方法,该方法会不显眼地动态地将多个实时
事件绑定到新行中的每个输入。
我需要从代码隐藏事件处理程序中创建的行获得相同的功能。我尝试通过 ClientScript.RegisterClientScriptBlock 和 ClientScript.RegisterStartupScript 简单地调用相同的方法,但它们都不起作用,并且代码隐藏行上没有事件触发(但它们按预期运行如果我然后添加一个客户端行)。
知道如何处理这个问题吗?我是否应该在页面的其他位置而不是在客户端创建方法中调用客户端绑定代码,以便它会在代码隐藏中触发?或者有另一种方法可以从代码隐藏中调用它吗?
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 expected 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个在页面初始化期间批量解析行的启动脚本,如下所示:
因此,从服务器端,您需要呈现一个批量处理表的脚本,然后以单一方式管理客户端交互。
你应该能够渲染出这样的东西;如果不能,请发布一些代码。
HTH。
Create a startup script that parses the rows in bulk during page initialization as in:
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.
如果您使用
live
并且所有行都共享一个公共类,那么它们如何添加并不重要,事件应该应用于它们。只需在页面加载时运行一次即可。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.