Print Page | Close Window

Accessing form variables in user control

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=17389
Printed Date: 28 March 2026 at 2:24am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Accessing form variables in user control
Posted By: sabrin514
Subject: Accessing form variables in user control
Date Posted: 29 November 2005 at 11:44am
Good morning,
 
I have a form in an aspx page which contains 2 user controls. These two user controls each contain items normally found in a form (such as text boxes). The submit button (and event) is located in the .aspx page.
 
My problem is that I cannot seem to access the values contained within the text boxes located in the user controls when I go to submit the form.
 
Any help would be greatly appreciated. Been bashing my head on the desk all morning :(
 
Thanks!



Replies:
Posted By: michael
Date Posted: 29 November 2005 at 3:42pm
Did you make the textbox private?
 
If in the User Control you do
Private x as new Textbox
it will not work
Do something like
Friend x as new Textbox
or
Public x as new Textbox
 
Then you should be able to read
 
Dim s as string = myControl.x.Text


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: sabrin514
Date Posted: 29 November 2005 at 3:53pm
Thank you very much for your response.
 
In my user Control  (fm_ex_provider_info.ascx) I have a textbox declared as:
 
Public txtBusinessName As New System.Web.UI.WebControls.TextBox
 
And related code:
 
Public ReadOnly Property ProvBusinessName() As String
    Get
        Return Me.txtBusinessName.Text
    End Get
End Property
 
-----------------------------------------------------------------
 
Then in my host form (ex_provider_input.aspx) I have:
 
Dim ProviderForm As fm_ex_provider_info1 = DirectCast(Me.ProviderForm, fm_ex_provider_info1)     [Declared in top variable section]
 
Dim BusinessName As String = ProviderForm.ProvBusinessName    [This piece of code is in the submit button event)
 
-----------------------------------------------------------------
 
 
The page loads just fine, but I get an error about the object not being set to an instance of the object when I hit the sub,it button. The error appears to be caused by this line of code" "Dim BusinessName As String = ProviderForm.ProvBusinessName ".
 
Apparently I am missing something. If anyone has any ideas, I would be very grateful. Thank you.
 
 
 
 


Posted By: sabrin514
Date Posted: 30 November 2005 at 10:06am
Problem was fixed by the following:
 
Dim ProviderForm As fm_ex_provider_info1  in variable declaration section
 
 
Then in page load:
 
ProviderForm = DirectCast(page.findcontrols("fm_ex_provider_info1", fm_ex_provider_info1))
 
 
Finally in submit button event:
 
Dim BusinessName As String = ProviderForm.ProvBusinessName  
 
Response.Write(strBusinessName)  'For testing purposes
 
 
Thanks to everyone that was looking into this.
 
 
 
 
 
 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net