How do you refer to a textbox that is located in a User Control?

M

Mike Hnatt

How can I refer to MyTextBox.Text which is located in mycontrol.ascx from
within mymainpage.aspx? (i.e, mycontrol.ascx is a User Control located on
mymainpage.aspx).

Thanks!
Mike
 
W

Webdiyer

Make it a public property and you can refer to it from within container page
by using this property.Hope it helps,thanks!
 
M

Mike Hnatt

Thanks Webdiyer,
But can anyone give me a hint as to what I need to do in order to do this?
Mike
 
M

Mike Hnatt

Thank John,

Here is what I would "think" would work:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
MyMainPageLabel.Text = "This is the first time viewing the page"
Else
MyMainPageLabel.Text = "The textbox value in the control is: " &
Mycontrol1.MyControlTextBox.Text
End If
End Sub

Based on a control called:
<uc1:mycontrol id="Mycontrol1" runat="server"></uc1:mycontrol>

Which in this user control, has a textbox:
<asp:TextBox id="MyControlTextBox" runat="server">Somevalue</asp:TextBox>

I have also tried refering to it as:
mycontrol.MyControlTextBox.Text

But I get a message saying it is inaccessible because it is "protected".

Thanks for the help!

Mike
 
W

Webdiyer

Hi,Mike,add a public property to your user control like this:

public string TextBoxText{
get{return MyControlTextBox.Text;}
set{MyControlTextBox.Text=value;}
}

then you can use MyMainPageLabel.Text=Mycontrol1.TextBoxText from within you
container page.Note I'm using C#!
 
M

Mike Hnatt

Perfect, thanks a lot. It worked! PS, I had to remember to add the line:
Protected WithEvents Searchcontrol1 As myproject.mycontrol

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top