0

I'm basically trying to get webcontrols to talk to one another.

I have one WUC that wants to get information from another.

In ControlB's GetDataFromControlA method

public List<ControlBData> GetDataFromControlA()
{
    ControlA c = Page.FindControl( IdOfControlA) as ControlA;
    if( c != null)
    {
        return c.Data;
    }
   ...
}

At code time ControlB knows nothing of ControlA...so ControlB cant access its members, and the above wont compile.

I'm thinking I have to use methods in the page to get the controls talking to one another...

Mesh
  • 6,262
  • 5
  • 34
  • 53
  • hmmm- its actually Intellisense that knows nothing of the Classes the code compiles and runs! - What to do with the points? – Mesh Feb 08 '11 at 13:42
  • have you tried my code? It does what you want... – Oskar Kjellin Feb 08 '11 at 13:43
  • Looking into it more it more that VS cant compile the project it complains of type not found, the site will compile and run under IIS or the dev server.... – Mesh Feb 08 '11 at 13:53
  • Ah ha! Adding <%@ Reference Control="~/ControlA.ascx" %> to ControlB.ascx 'fixes' the intellisence and the compile error! Found here http://stackoverflow.com/questions/1290592/dynamically-load-a-user-control-ascx-in-a-asp-net-website/1290609#1290609 – Mesh Feb 08 '11 at 14:02

2 Answers2

0

I was missing the <%@ Reference Control="~/ControlA.ascx" %> in ControlB.ascx

This 'fixes' the intellisence and the compile error! Found here

Community
  • 1
  • 1
Mesh
  • 6,262
  • 5
  • 34
  • 53
-1

Yes, you can use methods to achieve some communication between controls (since each belongs to a class in the end).

but you have to explicitly cast the Control to your own WUC's Datatype.

Example?

// Convert it from Control to Button
Button nextBtn = (Button)this.FindControl("ButtonIDHere");

// Make sure it is there (not null)
if (nextBtn != null)
{
    // Finally, let your logic does the magic!
    nextBtn.OnClientClick = "showPopup();";

    // Notice that here you can get the specific control members in addition to its base classes' members
}

You shouldn't use static in such case, it will get you a lot of headache.

If the WUC were in another Page, just get a reference to that page.

Hope that helps.

Ken D
  • 5,880
  • 2
  • 36
  • 58
  • -1 for two reasons. First of all the reason the above is failing is not because it is not an explicit cast. The "as" operator is just as valid in this case. Also, in this case when you reference "this" in "FindControl" you are referencing the UserControl and all it's children. The other UserControl is not a children of the first UserControl so your code will fail – Oskar Kjellin Feb 08 '11 at 13:31
  • I was just writing a comment :) Calm down – Oskar Kjellin Feb 08 '11 at 13:31
  • @Oskar, Well thanks for your explanation, but obviously I was including code-behind in the container `Page` of the WUC, not the control it self, that's why I said (If the WUC were in another Page, just get a reference to that page). I know that `as` operator is valid here and my way too :) – Ken D Feb 08 '11 at 13:44
  • What do you mean with "I was including code-behind in the container Page"? When you call "this" from inside the control you are calling the usercontrol object. If your are not inside the UserControl you are not answering the question – Oskar Kjellin Feb 08 '11 at 13:48