开发者

Calling Methods in a User Control from another page dynamically

开发者 https://www.devze.com 2023-01-05 13:53 出处:网络
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.

I´m writing a page that need to call an User Control Method dynamically.

  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() { ... }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消