I´m writing a page that need to call an User Control Method dynamically.
I have a GridView on my page and on the RowDataBound I´m loading this User Control for each row.
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()
{
...
}
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.
精彩评论