Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional 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 March 24th, 2005, 07:32 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Interacting User Controls

Hello Everyone and thanks for your help in advance. I have a web form with two user controls, lets call them Control1 and Control2. Within Control1, I have a button_click event that I want to have change the visibility of a panel in Control2. I have tried numerous VB.Net syntax involving FindControl, but simply can't get a reference to the other control and it's panel. I think this can be done, but I'm not sure how to go about doing this. I have also tried using delegates, but can't seem to get that right either. Any help would be greatly appreciated. Thanks.


 
Old March 24th, 2005, 04:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You have to expose the panel as a property in control2. You can't access the controls in another user control directly.

Brian
 
Old March 24th, 2005, 05:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Although it defeats the purpose of User Controls because you add a dependency between multiple controls, I think you can do something like this from within Control2:

Page.FindControl("Control1").Visible = false;

This hides the entire Control1, but obviously you could access other stuff as well using FindControl on the returned control, or by accessing the Controls collection.

The Page property of the control is a reference to the page the control is used on, so basically you can access whatever you want in the "parent" page.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: 01 - Ambulance by Blur (From the album: Think Tank) What's This?
 
Old March 25th, 2005, 06:49 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Hope all is well with you and thanks for the help. I hear what you are saying about defeating the purpose. I was hoping to set up some type of delegate that would allow other controls to subscribe (or not subscribe), but I can't seem to get that part figured out. Basically, I have two every similar controls, each with a datagrid of listitems and a detail page of each item. When a user clicks on the details of one item, I want to hide the datagrid of listitems on both controls. I tried having each control within a panel on the main page and referencing the panel using page.findcontrol, but that didn't quite work the way I wanted it. Any feedback on the best way to accomplish this?

 
Old March 27th, 2005, 06:11 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Hugh,

I think the best way to set this up is with event handlers on the controls.

1. In each control, define a delegate like this:
Code:
  public delegate void ChangeEventHandler(object sender, ChangeEventArgs e);
2. In each control, define a public event for the parent page to subscribe to:
Code:
  public event ChangeEventHandler Change;
3. Define a method that triggers the event from within your code:
Code:
  protected virtual void OnChange(ChangeEventArgs e)
  {
    if(Change != null)
    {
      Change(this, e);
    }
  }
  4. Call this method from anywhere in your code where it's appropriate; e.g. in the Button_Click or Selected_Index changed of your grid, for example:
Code:
  // Set up the EventArgs
Code:
  ChangeEventArgs myNewEventsArgs = new ChangeEventArgs();
  myNewEventsArgs.SomeProperty = SomeValue;

  // Raise Event
  OnChange(myNewEventsArgs);
  5. In this example, I used a custom class ChangeEventArgs. This is a simple class that inherits from System.EventArgs and has an additional property called SomeProperty.

If you don't need to pass additional info to the subscribers of the event, you can also use a normal System.EventArgs object as the parameter for your methods.

6. Next, in the parent page, set up the event for your control:
Code:
  this.MyControl1.Change += new MyControl.ChangeEventHandler(this.MyControl1_OnChange);
7. And finally, set up the method to do something useful, like call a method on the other control:
Code:
  private void MyControl1_OnChange(Object sender, ChangeEventArgs e)
  {
    if(e.SomeProperty == SomeValue)
    {
       MyControl2.SomeMethod();
    }
    else
    {
      SomeOtherMethod();
    }
  }
  I think this is the cleanest way to do it, as there is no coupling between Control1 and Control2, and only a loosly coupling between the Controls and the parent page, which is fine.

IMO, this is better than using FindControl, because there is no coupling and you also no longer depend on the (hardcoded) names of Control1 and Control2.

Out of curiosity: didn't my FindControl solution work? I did a quick test with two controls and I managed to make this work. What went wrong?

If you want to find out more about controls and how they work, take a look at the book Server Controls and Components. It's not an easy book to read, but it provides valuable insight in how (custom / server / user) controls work.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls dcct84 C# 2 October 28th, 2007 07:20 PM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
User Controls..... Help please cowboy2066 General .NET 2 July 28th, 2004 09:22 AM
User Controls Duncan Pro VB.NET 2002/2003 2 December 1st, 2003 05:41 AM
User Controls Duncan Pro VB.NET 2002/2003 1 October 27th, 2003 12:51 PM





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