Wrox Programmer Forums
|
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
 
Old April 4th, 2006, 04:40 PM
Authorized User
 
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old April 4th, 2006, 11:30 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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)

 
Old April 5th, 2006, 04:15 PM
Authorized User
 
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 5th, 2006, 11:47 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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()


 
Old April 6th, 2006, 04:46 PM
Authorized User
 
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 6th, 2006, 10:22 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

so you got it working?

 
Old April 7th, 2006, 11:22 AM
Authorized User
 
Join Date: Apr 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I did. Thank you

Peter
 
Old April 7th, 2006, 03:54 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Ok great!!

 
Old May 5th, 2007, 06:15 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I had the same problem. As Peter said, we should specify usercontrol id and event name as same.

regards
kamz

 
Old May 5th, 2007, 05:37 PM
Authorized User
 
Join Date: Sep 2006
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to VerbatimBOT Send a message via Yahoo to VerbatimBOT
Default

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
401.3 Access denied due to Access Control List cforsyth .NET Framework 2.0 8 May 28th, 2009 01:56 PM
How to Access Control on a From from different cla [email protected] C# 2005 2 October 27th, 2007 05:51 AM
how to access a control from another page labelle ASP.NET 1.0 and 1.1 Basics 0 January 10th, 2007 04:23 AM
TemplateField control access cmw231 ASP.NET 2.0 Basics 0 July 11th, 2006 08:50 AM
Access the dynamic control using C# kapi.goel C# 19 February 2nd, 2006 07:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.