sj1187534 Posted June 10, 2003 Posted June 10, 2003 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. Quote
bungpeng Posted June 11, 2003 Posted June 11, 2003 I never do it before, but you can try this: If you usercontrol name is "myUsercontrol" Quote Protected WithEvents myUsercontrol_textbox1 As System.Web.UI.WebControls.TextBox [/Quote] I hope it work... :) Quote
sj1187534 Posted June 11, 2003 Author Posted June 11, 2003 Nope...that didn't 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. Quote
hrabia Posted June 11, 2003 Posted June 11, 2003 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 Quote A man and a dog have an average of three legs. Beaware of Statistics.
sj1187534 Posted June 11, 2003 Author Posted June 11, 2003 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. Quote
bungpeng Posted June 11, 2003 Posted June 11, 2003 try to combine the name with template id like what I mentioned..... Quote
sj1187534 Posted June 11, 2003 Author Posted June 11, 2003 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 Quote
sj1187534 Posted June 11, 2003 Author Posted June 11, 2003 Thanks a lot..... I am fighting with that for a long time. SJ Quote
bungpeng Posted June 12, 2003 Posted June 12, 2003 It should work like what hrabia said... Or you can create a "Property" in usercontrol refer to the textbox. If you still can't get, then properly you miss the basic thing.... I suggest you post or attach your source here let us identify if possible Quote
sj1187534 Posted June 12, 2003 Author Posted June 12, 2003 Hi Guys, That did work. As you said, I declared the usercontrol in the wrong way. But when I changed it to what "hrabia" suggested, it worked. Thanks a lot for responding. SJ Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.