[英]how to access a web control inside a userControl from aspx page
i have 2 textbox controls inside a usercontrol TextBoxUC.ascx 我在用户控件TextBoxUC.ascx中有2个文本框控件
i have a page.aspx that contains the usercontrol. 我有一个包含用户控件的page.aspx。 how can i get a reference to each textbox using javascript from page.aspx?
我如何使用page.aspx中的javascript获取对每个文本框的引用?
do you have access to modify the user control? 您有权修改用户控件吗? if so, you can add properties like Textbox1ClientID and Textbox2ClientID, which would return the client id for the respective controls.
如果是这样,则可以添加诸如Textbox1ClientID和Textbox2ClientID之类的属性,这将返回相应控件的客户端ID。
user control c# : 用户控制c#:
public string Textbox1ClientID { get { return this.textbox1.ClientID; } }
js on the page: 页面上的js:
var text1 = document.getElementById('<% =this.UserControl1.Textbox1ClientID %>');
if you can't modify the user control, you'll have to put he client id string together manually. 如果您无法修改用户控件,则必须手动将其客户端ID字符串放在一起。
js: js:
var text1 = document.getElementById('<% =this.UserControl1.ClientID %>_Textbox1');
$('#<%= userUC.FindControl("txtFname").ClientID %>')
您可以将属性ClientIdMode
控件的ClientIdMode
设置为static,并且可以轻松找到。
You can look for it by the Control Name which should be something like UserControl1_TextBox1. 您可以通过控件名称(类似于UserControl1_TextBox1)来查找它。
document.getElementByID('UserControl1_TextBox1'); document.getElementByID('UserControl1_TextBox1');
its really hard if you are using masterPage or in case of UserControl because it generate there own id, the best way you can access them is using JQuery, 如果您使用masterPage或使用UserControl会非常困难,因为它会生成自己的ID,最好的访问方法是使用JQuery,
Inside your UserControl give your textbox class Name 在您的UserControl内部,给您的文本框类Name
< asp:textbox id="_text01" class="textbox" runat="server" /> <asp:textbox id =“ _ text01” class =“ textbox” runat =“ server” />
and from JQuery you can access them 从JQuery可以访问它们
$(".textbox").addClass("borderStyle"); $(“。textbox”)。addClass(“ borderStyle”);
I hope this work for you 希望这项工作对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.