简体   繁体   English

如何在使用asp.net创建的Java脚本中查找动态控件

[英]How to find Dynamic control in Java Script created using asp.net

I have created dynamic controls in ASP.NET as following .. 我已经在ASP.NET中创建了动态控件,如下所示。

first i have created checkbox as 首先我创建了复选框

 CheckBox chkDynamic = new CheckBox(); 

Then 然后

 TextBox txtDynamic = new TextBox();
 txtDynamic.ID = "txtDynamic";

and these controls added in tablecell added in tableRow added in Table added in aspPanel(Only panel created at design page) 这些控件添加在tablecell中,而tableRow中添加,而TableRow中添加,aspPanel添加(仅在设计页面上创建面板)

Now what i need.. when checkbox is selected i want to clear the txtDynamic Textbox using JavaScript 现在我需要..当选中复选框时,我想使用JavaScript清除txtDynamic文本框

I tried following ways but not working.. 我尝试了以下方法,但没有起作用。

 chkDynamic.Attributes["onclick"] = "javascript:document.getElementById('" + txtDynamic.UniqueID + "').value='';";

also i tried with calling Method as 我也尝试用Method作为

 chkDynamic.Attributes.Add("onclick", "javascript:ClearText(this)");

but in this method following line giving me error not found "txtDynamic".. because the control added dynamicaly. 但是在此方法中,以下一行给我的错误未找到“ txtDynamic” ..,因为控件是动态添加的。

 document.getElementById("<%= txtDynamic.ClientID %>").value="";

Thanks in advance.... 提前致谢....

After lots of debug i fond the answer. 经过大量调试后,我喜欢这个答案。 What does ASP.Net do at the run time it changes the ClientID of the control with attaching the parent control some prefix so before i added the entire table at the end of the all the TR created now what i did to solve this i have added the table to panel as plDynamicControls.Controls.Add(tblGeneralControls); ASP.Net在运行时会做什么,它通过将父控件附加一些前缀来更改控件的ClientID,因此在我将所有表添加到创建的所有TR的末尾之前,我已经添加了解决此问题的方法面板显示为plDynamicControls.Controls.Add(tblGeneralControls); then i added following code and it works like charm 然后我添加了以下代码,它就像魅力

chkDynamic.Attributes["onclick"] = string.Format("yearSuffixCHK(this, '{0}')", txtDynamic.ClientID);
txtDynamic.Attributes["onchange"] = string.Format("yearSuffixTXT(this, '{0}')", chkDynamic.ClientID);

Have you ensured that your textbox has been loaded into the DOM when this script is getting executed? 执行此脚本时,是否已确保将文本框加载到DOM中?

I would recommend registering the uniqueID/Client id of the control in a javascript variable from server side. 我建议在服务器端的javascript变量中注册控件的uniqueID / Client ID。

Then on client side after the whole DOM is loaded bind the click event on the checkbox and then do respective work of cleaning up the textbox 然后,在客户端上,在加载整个DOM之后,在复选框上绑定click事件,然后执行清理文本框的相应工作

(you can ensure that the DOM has loaded either by writing the script at end of page or using document.ready in case you use jQuery) (您可以通过在页面末尾编写脚本或使用document.ready以确保使用jQuery来确保已加载DOM)

Thanks, 谢谢,
Pranav Kauhik 普拉纳夫·考希克(Pranav Kauhik)
pranavkaushik.wordpress.com pranavkaushik.wordpress.com

When are you inserting the controls? 您何时插入控件? If you do it in Page_Load it won't work. 如果您在Page_Load中执行此操作,它将无法正常工作。 I would suggest inserting the controls in Page_Init (or Page_PreInit) if you are not already. 如果您尚未将控件插入Page_Init(或Page_PreInit),则建议您这样做。

Also, use ClientID and not UniqueID because UniqueID can contain the colon character (:), which is not valid in the HTML ID attribute (and is not allowed in variable names in client-side script). 另外,请使用ClientID,而不要使用UniqueID,因为UniqueID可以包含冒号(:),这在HTML ID属性中无效(并且在客户端脚本的变量名中不允许使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM