Access properties of usercontrol

Mondeo

Centurion
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
Hi,

I have a user control dropped onto a web form, the user control contains four textboxes.

From the main form how can I access one of the text boxes on the user control (I just want to read its text property)

Also, while on subject how to you do it the other way round? How does the user control access the form properties?

Thanks
 
The easiest way of doing the 1st part is to expose the information from the user control as one or more properties, this way they can be accessed from the containing page.

As to doing it the other way round that would be a bit more complicated - what kind of information would the control need to know about the page it is on?
 
Cheers,

How do I expose the textbox1.text property? Or would I need to do something like the following

Class UserControl

Public Property TextBox1Value as string
.....
End Property

Private Sub TextBox1.TextChanged handles TextBox1.TextChanged
me.TextBox1Value = TextBox1.Text
End Sub

End Class

Regarding the 2nd point, I wanted to bind to a datagrid on the main page based on the values in the usercontrol.

Thanks
 
Exposing individual TextBox properties or actual TextBox

You can either expose individual TextBox properties as required:

Visual Basic:
Public Property TextBoxText As String
    Get
        Return myTextBox.Text
    End Get
    Set
        myTextBox.Text = Value
    End Set
End Property

Or you could expose the TextBox itself, allowing external code to access all the properties, methods, and events of the TextBox:

Visual Basic:
Public ReadOnly Property MyTextBox As TextBox
    Get
        Return myTextBox
    End Get
End Property

Which you use depends upon how much of the TextBox you wish to expose.

Good luck :cool:
 
Sorry i'm wrong it doesn't work, the property is not visible in intellisense.

This is in my capsearch.asmx user control

Public ReadOnly Property CapCode() As String
Get
If ddlDerivative.SelectedValue <> -1 Then
Return ddlDerivative.SelectedValue
Else
Return "0"
End If
End Get
End Property

Then I have declared the usercontrol on the web form like so

<div id="wrapper">
<div id="cl">
<CAP:CAPSearch ID="CAPSearch" runat="server"/><br clear="all"/>
</div></div>

In my code behind for the page I can access CapSearch, theres loads of standard properties like Application, Attributes, Cache etc - but no CapCode.

What have I done wrong?

Thanks
 
Intellisense in ASP.Net

Does it work if you type CapCode anyway (without intellisense)? I've often experienced intellisense quirks in ASP.Net. Sometimes rebuilding the project helps, but not always.
 
Back
Top