0

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;

        }   
    }
P_R
  • 356
  • 2
  • 7
  • 27
  • Where and how are you bind your list view in codebehind? Are you rebind it on every page load? – Dmytro Rudenko Dec 23 '13 at 10:23
  • I bind my ListView on the onLoad event (with the function "carica_ListView) and It fire on every page load – P_R Dec 23 '13 at 10:30
  • Now I moved the "carica_ListView" function to the init event and the ItemCommand event works – P_R Dec 23 '13 at 10:41
  • After rebind process your ListView lost its ViewState, so it can't find right control for firing ItemCommand event. You may avoid this in 2 ways - 1) bind your ListView only on first page load (if (!Page.IsPostBack) { bind data }), or - 2) move your ListView binding to the Page.Init event handler. In this case ListView viewstate has been applied to the ListView correctly. – Dmytro Rudenko Dec 23 '13 at 10:43
  • post your page load code. – Raghubar Dec 23 '13 at 10:52

1 Answers1

1

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.

Jhabar
  • 109
  • 10