简体   繁体   English

为什么不能在Repeater中访问UserControl自定义属性?

[英]Why can't I access to a UserControl custom property inside a Repeater?

I'm doing a Data Binding for a Repeater: 我正在为中继器进行数据绑定:

<asp:Repeater ID="rptAlumni" runat="server" OnItemDataBound="rptAlumni_DataBinding">
    <ItemTemplate>
        <uc1:AlumnoBox runat="server" ID="AlumnoBox" />
    </ItemTemplate>
</asp:Repeater>

protected void rptAlumni_DataBinding(object sender, RepeaterItemEventArgs e)
{
    var item = (UserControl)e.Item.FindControl("AlumnoBox");
}

but I can't do item.MyCustomID for example, I can't see that property. 但我无法执行item.MyCustomID ,例如,我看不到该属性。 Instead, if I move the UserControl outside the Repeater and I do AlumnoBox.MyCustomID inside the Page Load it works as well. 相反,如果我将UserControl移到Repeater之外,并且在页面加载中执行AlumnoBox.MyCustomID ,它也可以正常工作。

Where am I wrong? 我哪里错了?

Your UserControl inherits from UserControl but you have added addtional properties like MyCustomID . 您的UserControl继承自UserControl但添加了其他属性,例如MyCustomID Of course you can't access those properties if you cast to UserControl since the base class does not know them. 当然,如果您强制转换为UserControl则不能访问这些属性,因为基类不知道它们。 Instead cast to the right type AlumnoBox : 而是AlumnoBox转换为正确的AlumnoBox类型:

var item = (context_box_AlumnoBox) e.Item.FindControl("AlumnoBox"); 
int id = item.MyCustomID;

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

相关问题 如何在Webpart的用户控件中引用中继器 - How can I reference a repeater inside a usercontrol inside a webpart 使用ItemTemplate(中继器)内部的自定义控件,如何访问中继器项目中的值? - With a custom control that's inside of an ItemTemplate (Repeater), how can I access values from the repeater item? 无法访问Repeater Itemtemplate中的TextBox - Can't access TextBox inside Repeater Itemtemplate 为什么我不能访问HasPicture属性? - Why can't I access the HasPicture property? 如何更改用户控件内的依赖属性值? - How can i change dependency property value inside a usercontrol? 如果用户控件位于重复器中,则不会对其进行初始化 - Controls inside a Usercontrol don't get initialized if it's in a Repeater 如何在自定义UserControl中获取所有TextBox? - How can I get all TextBoxes inside of a Custom UserControl? 为什么不能直接访问已分配给WinForms Control.Tag属性的自定义对象的属性? - Why can't I directly access the properties of a custom object that I've assigned to a WinForms Control.Tag property? 为什么我无法访问派生类中的受保护属性 - Why i can't access protected property in the derived class 反思 - 为什么我不能访问这个HttpRequest属性? - Reflection - Why can't I access this HttpRequest property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM