且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.net中将Javascript与动态控件一起使用

更新时间:2023-09-14 13:26:10

仅仅是因为获得ID并不意味着它是正确的.调试脚本,查看源以验证您要使用的DOM元素的实际ID.

由于您没有提供关于此控件的含义或用法的任何线索,因此请记住ASP.NET会弄乱该名称.您在标记中分配的ID可能与浏览器中呈现的ID不一致.
Just because you get an id doesn''t mean it is correct. Debug you script, look at the source to verify the actual id for the DOM element you are trying to use.

Since you have given us no clue as to what this control is or how it is used, remember ASP.NET will mangle the name. The id you assign in the markup may not be the same as what is rendered in the browser.


服务器端ID和客户端端ID不一致.尝试这样的事情:
The server-side ID and the client-side ID are not the same. Try something like this:
document.getElementById("<%= serverSideControl.ClientID %>").disabled = true;



如果要查看示例客户端ID,只需运行Web应用程序,然后在IE中右键单击并选择查看源代码".这将向您显示ASP.Net渲染到的HTML.

另外,请勿对您看到的客户端ID进行硬编码...客户端ID可能会因多种因素而发生变化.只需使用ClientID属性即可动态获取客户端ID.



If you want to see an example client-side ID, just run your web application and right-click in IE and select "view source". That will show you the HTML that the ASP.Net renders to.

Also, do NOT hardcode the client-side ID you see... the client-side ID can change depending on a number of factors. Just use the ClientID property to get the client-side ID dynamically.