C# 从事件上的中继器检索同级控件

C# 从事件上的中继器检索同级控件,c#,repeater,C#,Repeater,我在中继器控件上有一个下拉列表,还有一个按钮 当我想要启用按钮时,该按钮将被禁用,直到在DropDownList上选择了有效的项目。不幸的是,我似乎无法做到这一点 找到repeater by:(.As()方法是(对象As T)的扩展方法,只是使强制转换更容易) sender.As().NamingContainer.Parent.As() 但是,我得到的转发器对我没有帮助,因为FindControl(字符串名)函数没有返回任何内容,并且在监视窗口中没有显示任何有用的内容 那么,如何从中继器上另

我在中继器控件上有一个下拉列表,还有一个按钮

当我想要启用按钮时,该按钮将被禁用,直到在DropDownList上选择了有效的项目。不幸的是,我似乎无法做到这一点

找到repeater by:(.As()方法是(对象As T)的扩展方法,只是使强制转换更容易)

sender.As().NamingContainer.Parent.As()
但是,我得到的转发器对我没有帮助,因为FindControl(字符串名)函数没有返回任何内容,并且在监视窗口中没有显示任何有用的内容

那么,如何从中继器上另一项的事件(本例中为下拉菜单\u SelectedIndex Changed)中获取中继器上的同级控件(本例中为ImageButton)

编辑

我终于锻炼好了

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

我想我对你的问题有了答案:

1.-我使用dropdownlist和按钮创建了一个中继器来进行测试:

 <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>

这样做的方法是询问控件,谁是它的父控件,也就是说,RepeaterItem,或者您可以使用NamingContainer(正如我最后所写的),在那里您可以询问内部的任何控件

我想我对你的问题有了答案:

1.-我使用dropdownlist和按钮创建了一个中继器来进行测试:

 <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>

这样做的方法是询问控件,谁是它的父控件,也就是说,RepeaterItem,或者您可以使用NamingContainer(正如我最后所写的),在那里您可以询问内部的任何控件

差不多在那里,我终于找到了sender.As().NamingContainer.As().FindControl(“ControlName”)将父项更改为NamingContainer,我将接受它作为答案。差不多在那里,我终于找到了sender.As().NamingContainer.As().FindControl(“ControlName”)将父项更改为NamingContainer,我将接受它作为答案
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;

        }

    }