Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Posted

Exposing individual TextBox properties or actual TextBox

 

You can either expose individual TextBox properties as required:

 

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

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

Posted

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.

Never trouble another for what you can do for yourself.
Posted

Rebuild all fixed it, thanks.

 

And i've figured out going by other way by using newcontrol = me.parent.findcontrol("control")

 

Cheers

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...