473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Accessing a control from another page

How do I access a control, like a placeholder, from another page, or another
user control that may be on the same page.

For instance, I have a web page with 2 user controls. One user controls
contains a placeholder to that will contain any error messages to be
displayed. If the first user control creates an error, I want to be able to
push that error to the 2nd control to display it at the placeholder.

I have tried
Dim ctrlError as new ucErrors
Dim lbl as new label
lbl.text = "Error has occurred"
ctrlError.ph.Controls.Add(lbl)

But I get an error saying "System.NullReferenceException: Object reference
not set to an instance of an object."



Nov 19 '05 #1
2 2036
I'm no expert in the subject, but I don't know of a way to get two user
controls to talk to each other directly.

One way to empower communication is indirectly through the parent page. For
example, you may have two user controls on a page, uc1 and uc2. If uc1 has
an error, have it create and event which will be received by the parent and
acted on by an event handler in the parent. The parent will have a direct
reference to uc2, so you can then set properties or do whatever you want
with uc2 in the event handler.

Also, your code snippet looks like you are trying to dynamically create an
instance of a user control. I've always done this using the loadcontrol
call, for example
Dim questionDisplayCtrl As Control =
LoadControl("../templates/questionListItemOptions.ascx")

What you are trying to do (Dim ctrlError as new ucErrors) looks more
suitable for a regular web server control, class, or a composite control.

"eagle" <ea***@yahoo.com> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl...
How do I access a control, like a placeholder, from another page, or
another user control that may be on the same page.

For instance, I have a web page with 2 user controls. One user controls
contains a placeholder to that will contain any error messages to be
displayed. If the first user control creates an error, I want to be able
to push that error to the 2nd control to display it at the placeholder.

I have tried
Dim ctrlError as new ucErrors
Dim lbl as new label
lbl.text = "Error has occurred"
ctrlError.ph.Controls.Add(lbl)

But I get an error saying "System.NullReferenceException: Object reference
not set to an instance of an object."

Nov 19 '05 #2
Yes, that's right. You have to go through the parent page because the two
controls don't know anything about each other.

However, you shouldn't need to create a new instance of the control in your
code, because the parent page should automatically create it when it loads
up the .aspx file and sees the @Control directive, <uc:Control> tag, etc. So
the best way to access the control is to use the parent page's Page.Controls
property. Here's an example...this is terrible VB but you'll get the idea
;-)

' Assuming we are in the first user control

Dim ctrlErrorId
ctrlErrorId = "MyUserControl" ' need a variable for the user control ID,
otherwise how does it know which one to find?

Dim ctrlError as ucErrors
ctrlError = Parent.FindControl(ctrlErrorId)
' ctrlError now contains a reference to the error control

Hope that helps!

--

Joshua Mitts
jo****@msn.com

"HoustonFreeways" <es*******@NOcomcastSPAM.com> wrote in message
news:Yo********************@comcast.com...
I'm no expert in the subject, but I don't know of a way to get two user
controls to talk to each other directly.

One way to empower communication is indirectly through the parent page.
For example, you may have two user controls on a page, uc1 and uc2. If uc1
has an error, have it create and event which will be received by the
parent and acted on by an event handler in the parent. The parent will
have a direct reference to uc2, so you can then set properties or do
whatever you want with uc2 in the event handler.

Also, your code snippet looks like you are trying to dynamically create an
instance of a user control. I've always done this using the loadcontrol
call, for example
Dim questionDisplayCtrl As Control =
LoadControl("../templates/questionListItemOptions.ascx")

What you are trying to do (Dim ctrlError as new ucErrors) looks more
suitable for a regular web server control, class, or a composite control.

"eagle" <ea***@yahoo.com> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl...
How do I access a control, like a placeholder, from another page, or
another user control that may be on the same page.

For instance, I have a web page with 2 user controls. One user controls
contains a placeholder to that will contain any error messages to be
displayed. If the first user control creates an error, I want to be able
to push that error to the 2nd control to display it at the placeholder.

I have tried
Dim ctrlError as new ucErrors
Dim lbl as new label
lbl.text = "Error has occurred"
ctrlError.ph.Controls.Add(lbl)

But I get an error saying "System.NullReferenceException: Object
reference not set to an instance of an object."


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tom Meuzelaar | last post by:
Hello: I'm using VB6 in VS enterprise. I'd like to place an HTML form inside a VB container, have a user fill out the form information, click a submit button, and then have the program capture...
4
by: John Holmes | last post by:
I'm using data to rename some web controls on a form that uses a repeater contol and so it can have mulitple instances of the same control set. The controls get renamed (thanks to Steven Cheng's...
2
by: Willy | last post by:
Hi All! Is there any ways to get access to the controls contained in another user controls from an ASP.NET page? For example I have... - User Control A |_ User Control B (which uses User...
1
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have added a web user control to my project and placed some common controls over there.In order to access the control from another web page i registered with it(aspx page) using register tag...
2
by: RKT | last post by:
(Web Developer Express 2005, CSharp, C#) I've searched high and low... If have the typical _Default page. In it is a MultiView that contains another MultiView - in both of those are Views...
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
3
by: Nathan Sokalski | last post by:
I have a validator that I wrote by inheriting from BaseValidator. At certain points in the code, I need to access other controls on the page containing the validator. I have the IDs of these...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
2
by: San24 | last post by:
Guys, Let me explain the application I have - Form > Main Tab Control > Main Tab Page > User Control > Sub Tab Control > Sub Tab Page > User Control > Contols/Text Box. Form - The main...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.