【发布时间】:2013-12-23 10:09:13
【问题描述】:
我创建了一个 ListView,我在其中插入了一列我想在 ItemCommand 事件中触发的按钮,但是,当我按下按钮时,我获得了一个页面加载但没有任何反应(事件 ItemCommand 不会触发) .
<asp:ListView ID="ListView_documenti" runat="server" OnLoad="carica_ListView" OnItemCommand="esegui_comando">
<LayoutTemplate>
<table id="Table1" runat="server" class="ListViewUCSS">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="" >
<tr id="Tr2" runat="server" class="ListViewUHEADER">
<th id="Th0" runat="server" style="width:40%">Nome File</th>
<th id="Th3" runat="server" style="width:20%">Vedi</th>
</tr>
<tr ID="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="ListViewUTENTI">
<td><asp:Label ID="nomeLabel" runat="server" Text='<%# Eval("nome") %>' /></td>
<td><asp:button ID="vediDocButton" runat="server" Text="Vedi documento" CommandName="vedi_doc" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="ListViewUTENTIALTERNATING">
<td><asp:Label ID="nomeLabel" runat="server" Text='<%# Eval("nome") %>' /></td>
<td><asp:button ID="vediDocButton" runat="server" Text="Vedi documento" CommandName="vedi_doc" /></td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr>
<td>Nessun documento caricato per il seguente trust.</td>
</tr>
</table>
</EmptyDataTemplate>
这是在 ItemCommand 事件中关联的代码隐藏部分
protected void esegui_comando(object sender, ListViewCommandEventArgs e)
{
ListViewItem item = e.Item;
Label etichetta = (Label)item.FindControl("nomeLabel");
etichetta = (Label)e.Item.FindControl("nomeLabel");
//a questo punto capisco che button ha scatenato l'evento
switch(e.CommandName)
{
case "vedi_doc":
//indirizzo la pratica verso la pagina di visione delle pratiche
Response.Redirect("../scarica_documento.aspx?n=" + etichetta+"&c="+cartella);
break;
}
}
【问题讨论】:
-
您在哪里以及如何在代码隐藏中绑定列表视图?您是否在每次页面加载时重新绑定它?
-
我将我的 ListView 绑定在 onLoad 事件上(使用函数“carica_ListView”)并在每个页面加载时触发
-
现在我将“carica_ListView”函数移到了init事件中,并且ItemCommand事件起作用了
-
在重新绑定过程后,您的 ListView 失去了它的 ViewState,因此它无法找到触发 ItemCommand 事件的正确控件。您可以通过 2 种方式避免这种情况 - 1) 仅在第一页加载时绑定您的 ListView (if (!Page.IsPostBack) { bind data }),或者 - 2) 将 ListView 绑定移动到 Page.Init 事件处理程序。在这种情况下,ListView 视图状态已正确应用于 ListView。
-
发布您的页面加载代码。
标签: c# asp.net listview itemcommand