【问题标题】:How can I obtain a reference to a control created inside a repeater?如何获取对在中继器内创建的控件的引用?
【发布时间】:2010-03-05 04:16:17
【问题描述】:

我在转发器中有一个名为 thumbviewer 的控件。我想将其 imageurl 设置为 代码。目前它是在 aspx 本身中完成的

<asp:Repeater ID="Repeater1" runat="server" >
                    <ItemTemplate>
                        <span style="padding:2px 10px 2px 10px">

                            <bri:ThumbViewer Id="Th1"  runat="server" ImageUrl='<%# Eval("Name", "images/{0}") %>' Height="100px" Width="100px"/>
                        </span>
                    </ItemTemplate>
                </asp:Repeater>

如何在代码中设置 ImageUrl?

【问题讨论】:

    标签: asp.net vb.net repeater


    【解决方案1】:
    protected void rpter_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ThumbViewControl control = e.Item.FindControl("Th1") as ThumbViewControl;
            if (control != null)
            {
               control.ImageUrl = "";
            }
        }
    }
    

    在 aspx 上

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="rpter_ItemDataBound" >
                        <ItemTemplate>
                            <span style="padding:2px 10px 2px 10px">
    
                                <bri:ThumbViewer Id="Th1"  runat="server" Height="100px" Width="100px"/>
                            </span>
                        </ItemTemplate>
    </asp:Repeater>
    

    我个人会怎么做。

    如果您希望获取它的数据,那么我相信 e.Item.DataItem(或类似的东西)会得到它。

    干杯,

    T

    【讨论】:

    • 快!添加空检查的公平竞争。这是一个有用的练习,即使你 100% 可以控制!
    • 它将有效地检查控件是否存在,以及是否存在该 id 的控件,即类型为“ThumbViewControl”或其他类型的控件,因此这是一种两种检查合二为一的方式。我确实喜欢“as”:)
    【解决方案2】:

    您的中继器有一个 onitemdatabound 事件。

    <asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"></asp:Repeater>
    

    在您的代码中,您可以有一个名为的事件处理程序

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // ensure that we only look in the item template for our control
        if(  e.Item.ItemType == ListItemType.Item)
        {
             ThumbViewer tv = (ThumbViewer)e.Item.FindControl("Th1");
             tv.ImageUrl = "images/"+((<The object type you are binding>)e.Item.DataItem).Name;
        }
    }
    

    这是访问绑定到中继器的数据的最正统方式。恕我直言

    HTH

    【讨论】:

    • 根据代码我更新了我必须写的内容来代替你正在绑定的对象类型。请帮助
    • 为了清楚起见,绑定到中继器的对象具有 Name 属性。当你铸造 e. Item.DataItem 对象到所需的类型,它应该看起来像这样: MyType myCoolType = (MyType)e.Item.DataItem;这是基本的方法。但要缩短此代码,您可以编写 tv.ImageUrl = ((MyType)e.Item.DataItem).Name;
    【解决方案3】:

    您需要找到中继器,然后查看控件:

    我在这里做类似的事情并将控件加载到中继器内的占位符中..

            if (ResultRepeater != null && ResultRepeater.HasControls())
            {
                foreach (Control oControl in ResultRepeater.Controls)
                {
                    if (oControl != null && oControl is RepeaterItem)
                    {
                        PlaceHolder oSuggestMorePlaceholder = (PlaceHolder) oControl.FindControl("SuggestMorePlaceholder");
    
                        if (oSuggestMorePlaceholder != null)
                        {
                            SuggestMoreTabbedControl oTabbedControl = (SuggestMoreTabbedControl) Page.LoadControl("controls/SuggestMoreControl.ascx");
                            if (oTabbedControl != null)
                            {
                                oSuggestMorePlaceholder.Controls.Add(oTabbedControl);
                            }
                        }
                    }
                }
            }
    

    【讨论】:

    • 老兄,你需要看看 FindControl() 方法
    • FindControl() 不是递归的吗?如果是的话有点贵。最好在下面建议的 onitemdatabound 事件解决方案之一中使用它。
    • codinghorror.com/blog/archives/000307.html 不,不幸的是它不是递归的......
    • 我认为你错了,@Gordon... FindControl() 递归的。如果您不相信我,请查看 Jeff 帖子 (CodingHorror) 的 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多