i have 2 textbox controls inside a usercontrol TextBoxUC.ascx
i have a page.aspx that contains the usercontrol. how can i get a reference to each textbox using javascript from page.aspx?
views:
41answers:
3You 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
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');