ASP.NET:使用ImageButton从ListView编辑项目

时间:2012-08-16 17:27:07

标签: asp.net vb.net listview imagebutton

我在使用ASP.NET的ListView控件时遇到了一些问题。我正在尝试为每个项目添加编辑选项,如果我使用带有CommandName =“Edit”的LinkBut​​ton,它可以工作,但是(这里是问题)我需要使用ImageButton而不是LinkBut​​ton,它不起作用虽然我读过这不是问题(http://www.4guysfromrolla.com/articles/061009-1.aspx)。我也尝试将ImageButton嵌套在LinkBut​​ton中,但它不起作用。

这是我的ImageButton的标记代码:

<asp:ImageButton ID="editCircleIcon" runat="server" CommandName="Edit" ToolTip="Edit this circle" AlternateText="edit" Width="20px" ImageUrl="~/Images/Circulos/edit_circle.png" style="float:right;" />

我也尝试在ListView的ItemEditing事件中放置一个断点,但它永远不会被访问。

有什么想法吗?

感谢!!!

PD:这是整个ListView:

<asp:ListView id="listOfCircles" runat="server" DataKeyNames="id" ItemPlaceholderID="circlePlaceHolder">
    <EmptyDataTemplate>
        <asp:label ID="noCircles" runat="server" Text="No circles defined" />
    </EmptyDataTemplate>

    <LayoutTemplate>
        <asp:Panel ID="circle" runat="server" BorderColor="Black" Width="100%">
            <asp:PlaceHolder runat="server" ID="circlePlaceHolder" />
        </asp:Panel>    
    </LayoutTemplate>

    <ItemTemplate>
        <asp:HiddenField ID="circleID" runat="server" Value='<%# Eval("id") %>' />
        <asp:Panel ID="circleHeader" runat="server" BorderWidth="2px">
            <asp:Image ID="circleIcon" runat="server" />
            <asp:Label ID="circleName" runat="server" Text='<%# Eval("nombre") %>'  />
            <asp:ImageButton ID="deleteCircleIcon" runat="server" ImageUrl="~/Images/Circulos/delete_circle.png" width="20px" style="float:right;" ToolTip="Delete this circle" CommandName="Delete" />
                <ajaxToolkit:ConfirmButtonExtender id="deleteCircleCBE" runat="server"
                                                   DisplayModalPopupID="deleteCircleMPE"
                                                   TargetControlID="deleteCircleIcon" />
                <ajaxToolkit:ModalPopupExtender ID="deleteCircleMPE" runat="server"
                                                BackgroundCssClass="modalBackground" 
                                                PopupControlID="deleteCirclePopup" TargetControlID="deleteCircleIcon"
                                                OkControlID="deleteCircleOk" CancelControlID="deleteCircleCancel"
                                                DropShadow="true" />
                <asp:Panel ID="deleteCirclePopup" runat="server" CssClass="PopUp" style="display:none">
                    <asp:ImageButton ID="deleteCircleClosePopup" runat="server" ImageUrl="~/Images/closePopupIcon.png" CssClass="closePopupImage" OnClientClick="$find('deleteCirclePopup').hide();" />
                    <br /><br /><br />
                    <asp:Label id="deleteCircleMssg" runat="server" Text="Are you sure do you want to delete this circle?" />
                    <br /><br />
                    <asp:LinkButton ID="deleteCircleOk" runat="server" Text="delete" />&nbsp;|&nbsp;
                    <asp:LinkButton ID="deleteCircleCancel" runat="server" Text="cancel" ClientIDMode="Static" />
                </asp:Panel>
                <asp:ImageButton ID="editCircleIcon" runat="server" CommandName="Edit" ToolTip="Edit this circle" AlternateText="edit" Width="20px" ImageUrl="~/Images/Circulos/edit_circle.png" style="float:right;" OnCommand="editCircleIcon_OnClick" />
                <asp:LinkButton ID="editCircleLinkButton" runat="server" CommandName="Edit" ToolTip="Edit this circle" style="float:right;" Text="Edit" />
        </asp:Panel>
        <asp:Panel ID="circleMiddle" runat="server" BorderWidth="2px">
            <asp:Panel ID="users" runat="server">
            <%--espacio para que salgan los usuarios del circulo--%>
            </asp:Panel>
        </asp:Panel>
        <asp:Panel ID="circleFooter" runat="server" BorderWidth="2px">
                <a href="#" onclick='showPopup(newUserPanelPopup, <%# Eval("id") %>)'><img src="/Images/Circulos/add_friend.png" width="20px" alt="Add user" style="margin: 0" title="Add a new user to this circle" /></a>
        </asp:Panel>
        <br /><br />
    </ItemTemplate>

    <EditItemTemplate>
        <asp:Panel ID="circleHeaderEditMode" runat="server" BorderWidth="2px" DefaultButton="editCircleOk" style="background-color:#DDD;">
            <asp:Image ID="circleIcon" runat="server" />
            <asp:TextBox ID="circleNewName" runat="server" Text='<%# Eval("nombre") %>' />
            <asp:Button ID="editCircleCancel" runat="server" Text="Cancel" style="float:right" CommandName="Cancel" />
            <asp:Button ID="editCircleOk" runat="server" Text="Update" style="float:right" CommandName="Update" />
        </asp:Panel>
        <br /><br />
    </EditItemTemplate>

</asp:ListView>

现在,我有两个编辑选项的控件,ImageButton不起作用和LinkBut​​ton。

再次感谢!

3 个答案:

答案 0 :(得分:0)

尝试这个怎么样。

OnCommand="ImageButton_Command"

这显示了如何连接它。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.commandname.aspx#Y0

答案 1 :(得分:0)

我在计算机上对此进行了测试,我发现如果你的代码隐藏方法是这样的

Private Sub editCircleIcon_Command(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs) Handles editCircleIcon.Command
    If e.CommandName = "Edit" Then
       ... //your code
    End If
End Sub

并使用您的图像按钮

<asp:ImageButton ID="editCircleIcon" runat="server" CommandName="Edit" ToolTip="Edit this circle" AlternateText="edit" Width="20px" ImageUrl="~/Images/Circulos/edit_circle.png" style="float:right;" />

执行了代码隐藏。如果您要发布背后的代码,我可能会更有帮助。

我希望这会有所帮助。

答案 2 :(得分:0)

好的,我已经解决了这个问题。我在每个PostBack上重新填充ListView。解决方案很简单:

If Not Me.IsPostBack Then
        ReFillList()
    End If

神秘的是,LinkBut​​ton似乎并不感到困扰,删除选项总是有效......如果有人知道原因,我很想知道原因。

谢谢大家!!