简体   繁体   English

在转发器中调用用户控件的方法?

[英]Calling a method for a user control in a repeater?

I have a user control that when a method is called dynamically adds images to an asp panel. 我有一个用户控件,当一个方法被调用时,它会动态地将图像添加到ASP面板中。 I have placed this control in a repeater. 我已将此控件放在中继器中。 I am using the following code to call the function. 我正在使用以下代码来调用该函数。 I have tried calling this function on page_load, databinding of the repeater, databinding of the item, and oninit. 我试过在page_load,转发器的数据绑定,项目的数据绑定和oninit上调用此函数。

foreach (RepeaterItem repit in rpter.Items)
        {
            MyUserControl uc = rpter.FindControl("mycontrol") as MyUserControl;
            uc.MyMethod("var1","var2");
        }

The method on the usercontrol gets called, however no images show up on the page or in the source view of the page. 用户控件上的方法被调用,但是在页面或页面的源视图中没有图像。 Where am I going wrong? 我要去哪里错了?

I'm having a hard time understanding what you're trying to do, so this answer may be off target. 我很难理解您要执行的操作,因此此答案可能无法实现。 Can you handle the repeater's ItemDataBound event, locate the user control in the current item, and call the method directly on the user control? 您可以处理转发器的ItemDataBound事件,在当前项目中找到用户控件,然后直接在用户控件上调用该方法吗?

Is the user control the same type for every repeater item? 用户是否为每个转发器项目都控制相同的类型? If so, you can cast the user control to its type instead of UserControl and call the method directly instead of getting the MethodInfo and invoking it: 如果是这样,则可以将用户控件转换为其类型而不是UserControl,并直接调用该方法,而不是获取MethodInfo并调用它:

UserControl uc = repit.FindControl("ucontrol") as MyUserControl;
if (uc != null)
{
    uc.MyMethod("var1", "var2", "var3", "var4");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM