Listvew ItemCommand not fire

521 views Asked by At

I have created a ListView in which I inserted a column of buttons that I would like to trigger in the ItemCommand event but, when I press the button I obtain a Page Load but nothing happens(the Event ItemCommand don't fire).

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

and this is the Code Behind part associated at the ItemCommand Event

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;

        }   
    }
1

There are 1 answers

0
Jhabar On

You can also change your onload event code as:

protected void carica_ListView(object sender, EventArgs e)
{
     if (!Page.IsPostBack)
     {
     //Your code
     }
}

This will prevent re-binding of ListView on postback.