tags:

views:

638

answers:

1

I have a DropDownList on a repeater control, as well as a button.

The button is disabled until a valid item is selected on the DropDownList, when I want to enable the button. 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)

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.

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

EDIT

I finally worked out

sender.As<ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName")
+1  A: 

I think i have the answer for your question:

1.-I create a repeater with the dropdownlist and button to do the 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 databind the repeater.

2.-I create the method 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.

netadictos
Almost there, I finally worked out sender.As<System.Web.UI.WebControls.ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName")
johnc
Change Parent to NamingContainer and I'll accept it as an answer
johnc