accessing controls within user controls

sj1187534

Well-known member
Joined
Jun 10, 2003
Messages
108
Location
Dallas, Houston, etc.etc.
Hi, How can we access the webcontrols within a user control.
For example, I created a user control that has two text boxes in it. Now, I want to read the text entered in these text boxes from the main page. How can we do that?

SJ.
 
I never do it before, but you can try this:

If you usercontrol name is "myUsercontrol"

Protected WithEvents myUsercontrol_textbox1 As System.Web.UI.WebControls.TextBox

I hope it work... :)
 
Nope...that didnt work!!
Its giving the following error:
---------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


If ((Not NewPassRows1_txtNewFirstPass.Text Is String.Empty) And (NewPassRows1_txtNewFirstPass.Enabled = True)) Then
Error is in the above line
passText = NewPassRows1_txtNewFirstPass.Text
End If
--------------------------------------------------------------------------

NewPassRows1 is the user control and txtNewFirstPass is a textbox in the control.


SJ.
 
Make your textbox public (not protected) in user control:
Public WithEvents txtNewFirstPass As System.Web.UI.WebControls.TextBox
Declare your user control in main page:
Public WithEvents NewPassRows1 As WebUserControl1

Now you can call text property of textbox:
NewPassRows1.txtNewFirstPass.Text
 
Hi....It did not work. It is saying that txtFirstNewPass is not a member of System.Web.UI.UserControl !!!!

I declared the text field txtNewFirstPass as Public in the usercontrol code-behind file and also declared the NewPassRows1 instance of NewPassRows in the main .aspx page as a UserControl.


SJ.
 
this is the error it is giving me when I did that:
==============================================
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 110: Dim passText As String, returnValue As Integer
Line 111: passText = curUser.Get_Pass()
Line 112: If ((Not NewPassRows1_txtNewFirstPass.Text Is String.Empty) And (NewPassRows1_txtNewFirstPass.Enabled = True)) Then
Line 113: passText = NewPassRows1_txtNewFirstPass.Text
Line 114: End If


\EditProfile.aspx.vb Line: 112
==============================================
I made the textbox in the usercontrol public and also declared the user control textbox like this:

Protected WithEvents NewPassRows1_txtNewFirstPass As System.Web.UI.WebControls.TextBox

SJ
 
It should work like what hrabia said...

Or you can create a "Property" in usercontrol refer to the textbox.

If you still cant get, then properly you miss the basic thing.... I suggest you post or attach your source here let us identify if possible
 
Back
Top