簡體   English   中英

帶有按鈕 ItemCommand 事件的列表視圖未觸發

[英]listview with button ItemCommand event not triggered

第一次使用ListView控件,我設置了一個CommandNameCommandName事件。

當我單擊按鈕時應該觸發ListView_ItemCommand ,但它不是。 當我點擊它時沒有觸發。

<asp:ListView ID="ListView" runat="server" itemcommand="ListView_onItemCommand" 
            onitemcommand="ListView_ItemCommand">
            <EmptyDataTemplate>
                <table runat="server" 
                    style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <ItemTemplate>
                <tr style="background-color:#DCDCDC;color: #000000;">
                    <td style="text-align: center">
                        <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    </td>
                    <td style="text-align: center">
                        <asp:Label ID="BirthdayLabel" runat="server" Text='<%# Eval("Birthday") %>' />
                    </td>
                    <td style="text-align: center">
                        <asp:Label ID="CourseNameLabel" runat="server" 
                            Text='<%# Eval("CourseName") %>' />
                    </td>
                       
                    <td style="text-align: center">
                        <asp:ImageButton ID="Edit" runat="server" ImageUrl="~/images/edit_icon.png" CommandName="Edit" OnClick="Edit_OnClick" />
                    </td>
                     <td style="text-align: center">
                         <asp:ImageButton ID="Delete" runat="server" ImageUrl="~/images/delete_icon.png" CommandName="Delete" CommandArgument="Delete" />
                    </td>
                </tr>
            </ItemTemplate>

protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandArgument.ToString() == "Delete")
        {
            Label1.Text = "Edit From Button";
        }
    }

嘗試將您的命令名稱更改為其他名稱

 CommandName="Edit"

IE

CommandName="MyEdit"

Command="Edit" 改為觸發ListView_ItemEditing命令。 我認為這將掩蓋 ItemCommand。 但是當我犯這個錯誤時,TBH 由於缺少事件處理程序而導致頁面崩潰。

所以解釋2

另一種解釋可能是您正在重新綁定網格。 這通常會導致事件似乎消失了。 檢查你沒有這樣做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM