scatman
scatman

Reputation: 14565

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

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?

Upvotes: 0

Views: 5581

Answers (5)

Daniel
Daniel

Reputation: 2998

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

Upvotes: 0

Sathya Moorthy
Sathya Moorthy

Reputation: 1

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

Upvotes: 0

lincolnk
lincolnk

Reputation: 11238

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');

Upvotes: 1

Moksha
Moksha

Reputation: 1030

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

Upvotes: 0

bechbd
bechbd

Reputation: 6341

You can look for it by the Control Name which should be something like UserControl1_TextBox1.

document.getElementByID('UserControl1_TextBox1');

Upvotes: 0

Related Questions