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

编程入门 行业动态 更新时间:2024-10-23 23:29:56
本文介绍了如何在ASP中将Javascript与动态控件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将JavaScript与动态控件配合使用, 但问题javascript方法无法在页面中看到动态控件,它给我以下错误: document.getElementById(id)为null或初始化一个对象 我将控件的ID正确传递给javascript方法,因为我使用alert来显示ID及其正确. 并且javascript方法中的所有代码都可以正常工作,除了这一行 document.getElementById(id) 这是我的方法:

Hi, I want to use javascript with dynamic controls, but the problem javascript method cannot see the dynamic control in the page, it give me error that: document.getElementById(id) is null or nit an object i pass the id of control to javascript method correctly, as i use alert to show the id and it right. and all code in javascript method work right except this line document.getElementById(id) this is my method:

function SpecificMethod(id) { // some code here work fine alert(id); // give me right id document.getElementById(id).disabled =true; // give error }

这是动态创建控件的代码:

and this is the code which create the control dynamically:

protected void Page_Load(object sender, EventArgs e) { LinkButton lbtnMyLink = new LinkButton(); lbtnMyLink.Text = "Hello world"; lbtnMyLink.ID = "lbtnMyLink_1"; lbtnMyLink.OnClientClick = "SpecificMethod(" + lbtnMyLink.ClientID + ");return false"; divTest.Controls.Add(lbtnMyLink); }

推荐答案

仅仅是因为获得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.

更多推荐

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

本文发布于:2023-11-14 13:38:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   中将   动态   如何在   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!