Search code examples
c#asp.netpublic-method

Blindly calling a public method (C#)


OK, weird one. I have many usercontrols with a repeater, the layout of the repeater is the same in all controls, and they all have a bindData() method publically available.

I'm wondering, can I setup another usercontrol for paging without having to specify the parent control?

I'm able to do the following:

((controls.specificuserControlClass)Parent).bindData();

Which is all fine - however I'd need to specify the specificuserControlClass into the pager and then would need it "per repeater" if you see what I mean?

So can I call Parent.bindData() blindly from the child control? I "know" that method exists (or would build checks to make sure), however Visual Studio isn't happy as it doesn't know of the method.


Solution

  • Why not make your controls all implement a specific interface?

     public interface IBindData
     {
          void bindData();
     }
    

    Then, you would simply do:

    ((IBindData)Parent).bindData()
    

    And it should invoke each control's method as appropriate.