The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Controlling a Usercontrol from another Usercontrol

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
David Cumps

Posts: 319
Nickname: cumpsd
Registered: Feb, 2004

David Cumps is a Belgian Student learning .NET
Controlling a Usercontrol from another Usercontrol Posted: May 14, 2004 7:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by David Cumps.
Original Post: Controlling a Usercontrol from another Usercontrol
Feed Title: David Cumps
Feed URL: http://weblogs.asp.net/cumpsd/rss?containerid=12
Feed Description: A Student .Net Blog :p
Latest .NET Buzz Posts
Latest .NET Buzz Posts by David Cumps
Latest Posts From David Cumps

Advertisement
Today I received a question on how to make a usercontrol visible from another usercontrol. You could also see this as: "How could a usercontrol control everything from another usercontrol?"

Normally you wouldn't do this kind of stuff (at least not in my opinion). Usercontrols are to ASP.NET what modules were for VB6, and just like another class, they should function without knowing what's outside their borders, like small functional containers.

Let's take a look at this. First I created a normal page, Default.aspx, and I also made two usercontrols, Control1.ascx and Control2.ascx.

Control2.ascx only has a label, and Default.aspx contains the two usercontrols (named UControl1 and UControl2). Control1.ascx has a button on it, called cmdMakeVis.

First I tried making UControl2 as a public property in Default.aspx, and accessing it from UControl2 through there, unfortunately that gave me an object reference not set error.

My second attempt turned out fine though:

I took the Default.aspx page out of the Context.Handler and searched for UControl2 on it. If I would find it, I could control it. Good thing it worked ;)

Here's the code the button contains:
1private void cmdMakeVis_Click(object sender, System.EventArgs e) { 
2 TestSite.DefaultPage Default = (DefaultPage)Context.Handler;
3 TestSite.Control2 UserControl2 = (Control2)Default.FindControl("UControl2");
4 if (UserControl2 != null) {
5 UserControl2.Visible = true;
6 }
7} /* cmdMakeVis_Click *
I also uploaded this small test project as an illustration.

Read: Controlling a Usercontrol from another Usercontrol

Topic: Eric Lippert leverages the power of search engines Previous Topic   Next Topic Topic: Get your hands off my queue

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use