简体   繁体   中英

CommandName and CommandArguments are not properly working in ListView

I am facing problems while using the ListView Control in asp.net

I used two buttons links in a ListView ItemTemplate . For both buttons, I used Command Name and Command Argument. But first one is working fine and the second one is giving errors. Ie

System.InvalidOperationException: Insert can only be called on an insert item. Ensure only the InsertTemplate has a button with CommandName=Insert.

If I want to add the InsertTemplate , where do we have to place it?

I am copying my code. Please help me.

Design View :

<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" GroupItemCount="2" OnPagePropertiesChanging="ListView1_PagePropertiesChanging" DataKeyNames="InventoryID" OnItemCommand="ListView1_ItemCommand">
    <LayoutTemplate>
        <table width="100%">
            <tr style="background-color:lightblue;color:blue;text-align:center;font-size:25px;font-weight:bold">
                <td colspan="2">Available Books</td>
            </tr>
            <asp:PlaceHolder ID="groupPlaceHolder1" runat="server"></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr>
            <asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
        </tr>
    </GroupTemplate>
    <ItemTemplate>
        <td>
            <table cellpadding="2" cellspacing="0" border="1" style="width:100%;height:100px; border:dashed 1px #04AFEF;background-color:#B0E2F5">

                <tr>
                    <td>
                        <asp:Button ID="btnReview" runat="server" Text="Review" CommandName="Select" CommandArgument='<%# Eval("InventoryID") %>'/>
                    </td>
                    <td></td>
                    <td>
                        <asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Insert" CommandArgument='<%# Eval("InventoryID") %>' />
                    </td>
                </tr>
            </table>
        </td>
    </ItemTemplate>
</asp:ListView>            

Well you can insert a InsertItemTemplate like here :

<InsertItemTemplate>
    <tr style="background-color:#D3D3D3">
      <td valign="top">
        <asp:Label runat="server" ID="FirstNameLabel" 
          AssociatedControlID="FirstNameTextBox" Text="First Name"/>
        <asp:TextBox ID="FirstNameTextBox" runat="server" 
          Text='<%#Bind("FirstName") %>' /><br />
        <asp:Label runat="server" ID="LastNameLabel" 
          AssociatedControlID="LastNameTextBox" Text="Last Name" />
        <asp:TextBox ID="LastNameTextBox" runat="server" 
          Text='<%#Bind("LastName") %>' /><br />
        <asp:Label runat="server" ID="EmailLabel" 
          AssociatedControlID="EmailTextBox" Text="E-mail" />
        <asp:TextBox ID="EmailTextBox" runat="server" 
          Text='<%#Bind("EmailAddress") %>' />
      </td>
      <td>
        <asp:LinkButton ID="InsertButton" runat="server" 
          CommandName="Insert" Text="Insert" />
      </td>
    </tr>
  </InsertItemTemplate>

Used From : https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.insertitemtemplate(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM