简体   繁体   English

如何从aspx页面访问userControl内部的Web控件

[英]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.

相关问题 如何从aspx页面访问用户控件ID? - How to access usercontrol id from the aspx page? 为什么无法在.aspx页中访问usercontrol中的控件? - Why a control inside usercontrol is not accessible in .aspx page? 如何从asp.net的父aspx页中找到保留在另一个usercontrol中的用户控件? - How to find user control kept inside another usercontrol from parent aspx page in asp.net? 当控件从ASPX页面传递时,用户控件内的复选框将变为null - Checkboxlist inside a usercontrol going as null when control passed from an aspx page 如何从aspx页面访问silverlight控件的属性和方法? - How to access a silverlight control's properties and methods from an aspx page? 如何从父aspx页面访问子用户控件中的控件 - how to access the controls in child user control from parent aspx page 如何从我的aspx页面访问.ascx控件 - How to access .ascx control from my aspx page 如何以编程方式从CS文件访问ASPX页面上的控件 - How to programmatically access a control on a aspx page from the cs file 从另一个aspx页面访问aspx页面中的控件属性 - Access a control's property in an aspx page from another aspx page 如何从main.aspx页面的代码后面访问usercontrol.ascx中的字段集? - how to access fieldset in usercontrol.ascx from codebehind of the main.aspx page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM