Taking control of children from Repeater per event

I have a DropDownList to control the relay and also a button.

The button is disabled until a valid item is selected in the DropDownList if I want to enable the button. Unfortunately, I cannot figure it out.

Found a relay method: (.As () is an extension method for an object (like a T object), just makes casting easier)

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

      

However, the relay returned does not help me as the FindControl (string name) function returns nothing - and does not show anything useful in the viewport.

So how can I get a control (like an ImageButton in this case) on a repeater from an event on another repeater element (DropDown_SelectedIndexChanged in this case)?

EDIT

I have finally developed

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

      

+1


source to share


1 answer


I think I have an answer to your question:

1. -I am creating a repeater with a dropdown and a button to run tests:

 <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 am linking a repeater.



2. -I create a DropDownList1_SelectedIndexChanged method:

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 this is to ask the controller who is its parent what to tell the RepeaterItem, or you can use the NamingContainer (as I already wrote) and there you can ask about any control that is inside.

+4


source







All Articles