Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of TextBox from a UserControl

Status
Not open for further replies.

steverbs

Programmer
Jul 17, 2003
253
GB
Hi all.

Having a lttle trouble with UserControls. I am wanting to use UserControls to create reusable form elements, such as an address-entry set of TextFields etc. The only trouble is that I have no idea how to access them from a page that contains the control.

For example, if I have a WebForm called UserDetails, which contains an AddressDetails UserControl that is comprised of several text boxes, when the form is submitted, how do I access the values of the text boxes in the AddressDetails control?

Regards.

Stephen.
 
You should have something like this in your declarations on your main page:

Protected WithEvents ctrlpanel As ctrlpanel

In your control panel, try exposing some properties like this:

Code:
 Public nName As System.Web.UI.WebControls.TextBox
 Public nAddress As System.Web.UI.WebControls.TextBox

Then in your page load, you can put
Code:
nName = txtName
nAddress = txtAddress

On your main page, access the property as

ctrlPanel.nName.text
or
ctrlPanel.nAddress.text

This may not be exact syntax - I usually go the other way around making properties from the main page accessable to the control, so I am winging it here a little. But this hopefully will give you a direction, and intellisense should help out too. And I'll try to answer any questions you might have about this method.
 
Cheers for the reply.

I think I know where your coming from. I should have also pointed out that I am not adding the UserControls directly; they are being added to the page using placeholders, so that I code it like:

Dim objAddressForm As AddressDetails = LoadControl("AddressDetails.ascx")

Me.phAdminDetails.Controls.Add(objAdminForm)

Now, whenever I try to access the Public properties of the control within the placeholder, I get the error:

'PropertyName is not a member of 'PlaceHolder'.

I have tried accessing the property using:

Dim uclAdminForm As AddressDetails

uclAddressForm = Me.phDetails.FindControl("AddressDetails")

Is there a way to do this using PlaceHolders? I'll have a crack at just adding the UserControl directly to see if I can get away without the PlaceHolder.

Cheers.

Stephen.
 
OK. Thank you for your solution, it works a treat. I've also just realised that the whole placeholder thing won't work anyways.

Thanks again!

Steve.
 
One more thing now, if you don't mind. How do I set the properties of the UserControl from the WebForm so that when the UserControl loads, it can populate its text fields with data. I was trying something along the lines of:

(In the WebForm)

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Not Me.IsPostBack) Then
Me.uclAddressDetails.myAddress = Address.firstInstance()

But all I get is a Null Object Reference for the last line, as the control obviously hasn't been instantiated yet. However, whenever I try to instantiate the control using the New satement or LoadControl method, the property doesn't seem to get set.

What am I missing here?

Steve.
 
basically, make any properties that you want to change public in your user control. Then, in the web form, assign those properties a value. For example, if you have

Public nName As System.Web.UI.WebControls.TextBox

in your user control, you can access it through the web form like this:

ctrlPanel.nName

just as if it were any other text box.

So ctrlPanel.nName.text = "John Doe" will put "John Doe" in
your textbox.

"ctrlPanel.nName.visible = false" will hide your text box. ctrlPanel is declared like I did in my previous example. Does this make sense? If it didn't answer your previous question, please rephrase it and I'll try again.
 
Cheers for the reply.

I've just found out what the problem was. I had been trying as you suggested, to just change the properties via ctrlPanel.property, but it kept coming up with a null reference error. I found that it was because I needed to declare ctrlPanel as Protected instead of Private in the WebForm. Don't quite get why this should've changed anything as I was only acessing ctrlPanel from inside the WebForm anyways.

Thanks again.

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top