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

发布于 2024-10-13 03:27:38 字数 12234 浏览 5 评论 0原文

每个人! 我正在我的网页上编写中继器控件。在我为中继器添加数据分页后,我遇到了问题: 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>

everyone!
I'm writing repeater control on my web-page. After I added data paging for repeater I have got problem:
ImageButton does'nt fire any more OnItemCommand event of repeater. Why?

Here, is part of markup code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-10-20 03:27:38

我尝试了多种方法来解决这个问题并解决了:
我只是将 ImageButton 替换为 linkBut​​ton 和图像:
部分代码:

<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

I've tried nany ways to solve this problem and solved it so:
I just replace ImageButton with linkButton with image:
Part og code:

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

For more see: http://weblogs.asp.net/justinsaraceno/archive/2008/11/05/linkbutton-with-image-and-text.aspx

两人的回忆 2024-10-20 03:27:38

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

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

Sorry I don't have a more specific answer, but it has something to do with ViewState on the repeater and rebinding the repeater on Postback.

Apparently, when the postback occurs and you rebind the repeater, it loses some event binding and instead of running the OnCommand, it treats it as if it's a first page load for ImageButton. AFAIK it only happens on ImageButton and not LinkButton or Button.

寄居人 2024-10-20 03:27:38

将其放在后面的代码中肯定会起作用

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

Put this in code behind it will surely work

if (!Page.IsPostBack)
{
    REPEATERDATABIND(); // Bind method
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文