tags:

views:

32

answers:

1

Hello Masters!

I´m writting a page that need to call an User Control Method dinamically.

  1. I have a GridView on my page and on the RowDataBound I´m loading this User Control for each row.

  2. At my User Control I have a public void Method that I need to call when I load this Control.

I was reading about reflection, but at the code that I used, the method that I created on the User Control wasn´t found. See the code:

UserControl x = (UserControl)LoadControl("../usercontrols/ucResultData.ascx"); x.GetType().InvokeMember("CreateData", BindingFlags.InvokeMethod, null, this.Page, null);

This is the code on the User Control:

public void CriarGradeHorario() { ... }

Thank´s Masters!

Andrew

A: 

You are trying to invoke the method CreateData on the current Page object. You want to invoke the CriarGradeHorario method on the user control object:

x.GetType()
  .InvokeMember("CriarGradeHorario", BindingFlags.InvokeMethod, null, x, null);

However, if you want to call the method this way, you should consider moving it out of the user control and put it in a class file. That way you can call it without using reflection.

Guffa
Thank´s Master!
Andrew