Web User Control

fullyii

Member
Joined
Aug 19, 2005
Messages
22
Location
Chicago
Programming Experience
1-3
"Visual studio 2003 VB.net"

I created a simple Web user control. This control contains a Calendar control (calander1) and a textbox (textbox1).

The calendar populates the textbox with a selected date. Works fine in the destination aspx page.

Now I need to get to the textbox1.text property in order to do an insert into a database.

How do I access textbox1.text property from the aspx page?


any help would be greatly appreciated.
 
Last edited:
Add a public property to the web user control, example:
VB.NET:
Public ReadOnly Property tbText() As String
Get
  Return TextBoxUsercontrol.Text
End Get
End Property
From the web form use FindControl method to find the user control object, once you got the UC object get its property, example:
VB.NET:
Dim wuc As WebUserControl1 = Me.FindControl("WebUserControl1")
TextBoxWebform.Text = wuc.tbText
 
Note that you should access this property in the PreRender event, not Load event because the textbox value will not be processed until after the Load event completes, the post-data is processed, then prerender fires.
 
Thank you for responding I do have a question about the following:

TextBoxWebform.Text = wuc.tbText

can someone explain TextBoxWebform.text I am not sure what or how this works or get it to work.
 
"TextBoxWebform.Text" was an example assigning the string requested from the web user control (wuc). The intended meaning is "a Textbox on a webform that is named 'TextBoxWebform'". .Text is the property of a Textbox that displays the text value when you are viewing the Textbox in the form through a browser. You can also write text in the Textbox, what happens then is that the Text property changes to the text value of the text you have written in the Textbox. Hope this clarifies the example.
 
I am getting a Invalid cast error message.

Specified cast is not valid.

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif]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.InvalidCastException: Specified cast is not valid.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:

[InvalidCastException: Specified cast is not valid.] _172._17._7._80.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\nbksog8\VSWebCache\172.17.7.80\ccRequestor.aspx.vb:139 System.Web.UI.Control.OnLoad(EventArgs e) +67 System.Web.UI.Control.LoadRecursive() +35 System.Web.UI.Page.ProcessRequestMain() +750[/FONT]
Not sure what this means.

Also the prerender event could you give me an example of how this works?
 
Last edited:
Back
Top