我在转发器控件上有一个DropDownList,还有一个按钮。
当我想要启用按钮时,按钮被禁用,直到在DropDownList上选择了有效项目。不幸的是我似乎无法实现它。
通过以下方式找到转发器:(。As()方法是(对象为T)的扩展方法,只是简化了转换)
sender.As<Control>().NamingContainer.Parent.As<Repeater>()
然而,我回来的Repeater对我没有帮助,因为FindControl(字符串名称)函数没有返回任何东西 - 并且在监视窗口中没有显示任何有用的东西。
那么,如何从转发器上另一个项目的事件(在这种情况下为DropDown_SelectedIndexChanged)中获取转发器上的兄弟控件(在本例中为ImageButton)?
修改
我终于搞定了
sender.As<ImageButton>().NamingContainer.As<RepeaterItem>().FindControl("ControlName")
答案 0 :(得分:4)
我想我有你的问题的答案:
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>
我对转发器进行了数据处理。
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;
}
}
这样做的方法是向控件询问,谁是它的Parent,也就是说,RepeaterItem,或者你可以使用NamingContainer(正如我最后写的那样),你可以询问里面的任何控件。