|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
April 4th, 2006, 04:40 PM
|
Authorized User
|
|
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Access control from usercontrol
Hi,
Is there a way to access a web control such as textbox from a usercontrol (.ascx page). For example, I created a usercontrol which has a textbox and dropdownlist. I placed this usercontrol on a web form (test.aspx). How do I get the textbox value that I created in my usercontrol onto this web form test.aspx. I try myControl.Attributes.items("textboxName") but it does not seem to work.
Thanks in advance,
Peter
__________________
Peter
|
April 4th, 2006, 11:30 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Yes. For example, If your webusercontrol is named webusercontrol1:
declare at page level:
Protected WithEvents WebUserControl11 As WebUserControl1
Then get a reference to the textbox:
Dim tb As New TextBox
tb = WebUserControl11.FindControl("TextBox1")
Response.Write(tb.Text)
|
April 5th, 2006, 04:15 PM
|
Authorized User
|
|
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
In your example, "TextBox1" is the text box on the web user control page, right?
It keep returning Nothing; thus returning this error:
line 30: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 27: Dim x As Integer
Line 28: 'x = testusercontrol.Attributes.Count
Line 29: tb = testusercontrol.FindControl("txtUserID")
Line 30: Response.Write(tb.Text)[/red]
Thanks,
Peter
Peter
|
April 5th, 2006, 11:47 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
did you declare the page level variable and did you reference the correct control?
did you dim tb as new textbox?.. also make sure the control name is correct in the findcontrol()
|
April 6th, 2006, 04:46 PM
|
Authorized User
|
|
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks. Well, my problem was that I named the user control ID on the form different from what I declared on page level protected event.
Again, thank you for the help.
Peter
|
April 6th, 2006, 10:22 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
so you got it working?
|
April 7th, 2006, 11:22 AM
|
Authorized User
|
|
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes I did. Thank you
Peter
|
April 7th, 2006, 03:54 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Ok great!!
|
May 5th, 2007, 06:15 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I had the same problem. As Peter said, we should specify usercontrol id and event name as same.
regards
kamz
|
May 5th, 2007, 05:37 PM
|
Authorized User
|
|
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I usually do it like this: in my user control I declare a property
Code:
Public ReadOnly Property TextBox1() As TextBox
Get
Return TextBox1
End Get
End Property
Now, in order to access that TextBox, all you have to do is access this property like
Code:
MyUserControl.TextBox1
Best regards,
Aleksandar Dragosavac
|
|
|