开发者

how to access a web control inside a userControl from aspx page

开发者 https://www.devze.com 2023-01-03 20:59 出处:网络
i have 2 textbox controls inside a usercontrol TextBoxUC.ascx i have a page.aspx that contains the usercontrol. how 开发者_StackOverflow社区can i get a reference to each textbox using javascript from

i have 2 textbox controls inside a usercontrol TextBoxUC.ascx

i have a page.aspx that contains the usercontrol. how 开发者_StackOverflow社区can i get a reference to each textbox using javascript from page.aspx?


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.

user control c# :

public string Textbox1ClientID { get { return this.textbox1.ClientID; } }

js on the page:

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.

js:

var text1 = document.getElementById('<% =this.UserControl1.ClientID %>_Textbox1');


You can look for it by the Control Name which should be something like 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,

Inside your UserControl give your textbox class Name

< asp:textbox id="_text01" class="textbox" runat="server" />

and from JQuery you can access them

$(".textbox").addClass("borderStyle");

I hope this work for you


$('#<%= userUC.FindControl("txtFname").ClientID %>')


You can set the property ClientIdMode to static of any aspx controls and you can easily find.

0

精彩评论

暂无评论...
验证码 换一张
取 消