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

  • Thread starter Thread starter Mike Hnatt
  • Start date Start date
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
 
Make it a public property and you can refer to it from within container page
by using this property.Hope it helps,thanks!
 
Thanks Webdiyer,
But can anyone give me a hint as to what I need to do in order to do this?
Mike
 
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
 
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#!
 
Perfect, thanks a lot. It worked! PS, I had to remember to add the line:
Protected WithEvents Searchcontrol1 As myproject.mycontrol

Mike
 
Back
Top