有人可以告诉我为什么我无法访问位于代码后面的listView中的按钮吗?
这是我的清单
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="LinkButton3" runat="server" Text='<%# Eval("Name") %>' OnClick="LinkButton3_Click"/>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
我无法在页面加载
上执行此操作protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(LinkButton3);
}
linkButton3在当前上下文中不存在。任何帮助将不胜感激
答案 0 :(得分:0)
从您提供的代码中。
Button位于ItemTemplate中。您可以将ItemDataBound事件处理程序添加到ListView。然后,在那种情况下,您将能够访问按钮。
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="LinkButton3" runat="server" Text='<%# Eval("Name") %>' OnClick="LinkButton3_Click"/>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
然后在代码背后
protected void ListView1_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
//if this item is not being edited, find controls declared in ItemTemplate
if (ListView1.EditItem == null || (ListView1.EditItem != null && dataItem.DisplayIndex != ListView1.EditIndex))
{
Button btn = (Button)e.Item.FindControl("LinkButton3");
}
//if this item is being edited, find controls declared in the EditItemTemplate
if (ListView1.EditItem != null && dataItem.DisplayIndex == ListView1.EditIndex)
{
Button btn = (Button)ListView1.EditItem.FindControl("LinkButton3");
}
}