简体   繁体   English

在事件中从Repeater检索兄弟控制

[英]Retrieving sibling control from Repeater on event

I have a DropDownList on a repeater control, as well as a button. 我在转发器控件上有一个DropDownList,还有一个按钮。

The button is disabled until a valid item is selected on the DropDownList, when I want to enable the button. 如果我想启用按钮,则在DropDownList上选择有效项目之前,该按钮将被禁用。 Unfortunately I can't seem to get to it. 不幸的是我似乎无法达到它。

Found the repeater by: (.As() method is an extension method for (object as T), just makes casting easier) 通过以下方式找到转发器:(。As()方法是(对象为T)的扩展方法,只是简化了转换)

sender.As<Control>().NamingContainer.Parent.As<Repeater>()

However the Repeater I get back doesn't help me as the FindControl(string name) function isn't returning anything - and shows nothing useful in the watch window. 然而,我回来的Repeater并没有帮助我,因为FindControl(字符串名称)函数没有返回任何东西 - 并且在监视窗口中没有显示任何有用的东西。

So, how can I get a sibling control (an ImageButton in this case) on a repeater from an event of another item on the repeater (DropDown_SelectedIndexChanged in this case)? 那么,如何从转发器上另一个项目的事件(在这种情况下为DropDown_SelectedIndexChanged)中获取转发器上的兄弟控件(在本例中为ImageButton)?

EDIT 编辑

I finally worked out 我终于解决了

sender.As<ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName")

I think i have the answer for your question: 我想我有你的问题的答案:

1.-I create a repeater with the dropdownlist and button to do the tests: 1.-我创建一个带有下拉列表和按钮的转发器来进行测试:

 <asp:Repeater ID="rp" runat="server">
   <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
        <asp:ListItem>5</asp:ListItem>
        <asp:ListItem>6</asp:ListItem>

        </asp:DropDownList>
        <asp:ImageButton ID="Button1" runat="server" Enabled="False" />
        </ItemTemplate>
        </asp:Repeater>

I databind the repeater. 我对转发器进行了数据绑定。

2.-I create the method DropDownList1_SelectedIndexChanged: 2.-我创建方法DropDownList1_SelectedIndexChanged:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList control = (DropDownList)sender;

        RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
        if (rpItem != null)
        {
            ImageButton btn = ((ImageButton)rpItem.FindControl("Button1"));
            btn.Enabled = true;

        }

    }

The way to do it is to ask to the control, who is its Parent, that's to say, the RepeaterItem, or you can use NamingContainer (as I have written finally) and there you can ask about any control that is inside. 这样做的方法是询问控件,谁是它的Parent,也就是说,RepeaterItem,或者你可以使用NamingContainer(正如我最后写的那样),你可以询问里面的任何控件。

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

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