ImageButton不会在转发器控件中触发OnItemCommand

时间:2011-01-20 09:48:41

标签: asp.net repeater imagebutton

大家好! 我在我的网页上写了转发器控件。在我为转发器添加数据分页后,我遇到了问题: ImageButton不再触发转发器的OnItemCommand事件。为什么呢?

这里是标记代码的一部分:

 <asp:Repeater ID="PartsRepeater" runat="server"
                    OnItemCommand="partsRepeater_ItemCommand"
                    OnItemDataBound="partsRepeaterItem_Bound">
                    <HeaderTemplate>
                        <table rules="all" cellspacing="0" style="border-color: rgb(31, 77, 48);
                            border-style: solid; width: 100%; border-collapse: collapse;">
                            <tr >
                                <td style="width: 92%;">
                                    <table cellspacing="0" border="0" style="border-color: rgb(31, 77, 48); border-style: solid;
                                        width: 100%; border-collapse: collapse;">
                                        <tr style="background-color: #1f4d30; color: white;">
                                            <td style="width: 10%;">
                                                <asp:Label runat="server" Text="Name" ID="lblName">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 10%;">
                                                <asp:Label runat="server" Text="Number" ID="Label64">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 10%;">
                                                <asp:Label runat="server" Text="Category" ID="Label65">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 20%;">
                                                <asp:Label runat="server" Text="Description" ID="Label66">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 20%;">
                                                <asp:Label runat="server" Text="Models" ID="Label67">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;">
                                                <asp:Label runat="server" Text="Price" ID="Label68">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;">
                                                <asp:Label runat="server" Text="A Qty" ID="Label69">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;">
                                                <asp:Label runat="server" Text="B Qty" ID="Label70">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;">
                                                <asp:Label runat="server" Text="Actual Qty" ID="Label71">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;">
                                                <asp:Label runat="server" Text="Total Qty" ID="Label72">
                                                </asp:Label>
                                            </td>
                                            <td style="width: 5%;" >
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                <td style="width: 8%;" >
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr style="border-color: #1f4d30;"
                         >
                        <td style="width: 92%;">
                                <table border="0" cellspacing="0" class="PartsRepeater_InnerTable">
                                    <tr class="PartsRepeater_InnerTable">
                                        <td style="cursor: pointer; width: 10%;" class="PartsRepeater_InnerTable">
                                            <asp:HiddenField runat="server" ID="PartID" Value='<%# Bind("PartID") %>' Visible="false" />
                                            <asp:LinkButton runat="server" ID="Label52" Text='<%#Eval("Name").ToString()%>' CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 10%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="Label45" runat="server" Text='<%#Eval("Number").ToString().ToUpper()%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 20%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="Label46" runat="server" Text='<%#Eval("Category").ToString()%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 20%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="Label47" runat="server" Text='<%# GetPartDescr((String)Eval("Description"))%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 5%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="Label48" runat="server" Text='  <%# GetPartDescr((String)Eval("ModelName")) %>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 4%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="Label49" runat="server" Text='<%# GetPrice(Eval("Price").ToString(), Eval("sPrice").ToString(), Eval("Price3").ToString())%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 4%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="lblAQTY" runat="server" Text=' <%#Eval("AGradeQty").ToString()%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 4%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="lblBQty" runat="server" Text=' <%# int.Parse(Eval("QtyInStock").ToString()) - int.Parse(Eval("AGradeQty").ToString())%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 4%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="lblActualQTY" runat="server" Text=' <%#Eval("ActualQty").ToString()%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 4%;" class="PartsRepeater_InnerTable"> 
                                            <asp:LinkButton ID="Label60" runat="server" Text='<%#Eval("QtyInStock").ToString()%>'
                                                CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                    </tr>
                                    <tr >
                                        <td colspan="15" style="cursor: pointer; width: 5%;">
                                            <asp:LinkButton ID="Label63" runat="server" Text='<%#Eval("priceModificationDate").ToString()%>'
                                                Font-Size="Smaller" CommandArgument='<%# Eval("PartID") %>'>
                                            </asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td style="width: 8%">
                                <table>
                                    <tr>
                                        <td style="cursor: pointer; width: 50%;" class="PartsRepeater_InnerTable">
                                            <asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/Images/edit.png" CausesValidation="False"
                                                CommandName="editRow" CommandArgument='<%# Eval("PartID") %>' OnClientClick='SaveScrollPositions'>
                                            </asp:ImageButton>
                                        </td>
                                        <td style="cursor: pointer; width: 50%;">
                                            <asp:ImageButton ID="btnDelete" runat="server" OnClientClick='<%# GetConfirmMessage(Eval("Name"))%>'
                                                ImageUrl="~/Images/delete.png" CausesValidation="False" CommandName="Delete"
                                                CommandArgument='<%# Eval("PartID") %>'></asp:ImageButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

3 个答案:

答案 0 :(得分:2)

我尝试了很多方法来解决这个问题,并解决了这个问题: 我只是用linkBut​​ton替换ImageButton和image: 部分og代码:

<td style="cursor: pointer; width: 50%;" class="PartsRepeater_InnerTable">
                                            <asp:LinkButton ID="btnEdit" runat="server" CausesValidation="false" CommandName="editRow"
                                                CommandArgument='<%# Eval("PartID") %>' OnClientClick='SaveScrollPositions'>
                                                <asp:Image ID="Image1" runat="server" ImageUrl="~/Images/edit.png" />
                                            </asp:LinkButton>
                                        </td>
                                        <td style="cursor: pointer; width: 50%;">
                                            <asp:LinkButton ID="btnDelete" runat="server" CausesValidation="false" CommandName="Delete"
                                                CommandArgument='<%# Eval("PartID") %>' OnClientClick='<%# GetConfirmMessage(Eval("Name"))%>'>
                                                <asp:Image ID="Image2" runat="server" ImageUrl="~/Images/delete.png" />
                                            </asp:LinkButton>
                                        </td>

有关详情,请参阅:http://weblogs.asp.net/justinsaraceno/archive/2008/11/05/linkbutton-with-image-and-text.aspx

答案 1 :(得分:1)

抱歉,我没有更具体的答案,但它与转发器上的ViewState有关,并在Postback上重新绑定转发器。

显然,当回发发生并重新绑定转发器时,它会丢失一些事件绑定,而不是运行OnCommand,它会将其视为ImageButton的第一页加载。 AFAIK它只发生在ImageButton而不是LinkBut​​ton或Button。

答案 2 :(得分:0)

把它放在它背后的代码中肯定会有用

if (!Page.IsPostBack)
{
    REPEATERDATABIND(); // Bind method
}