locked
Listview ItemCommand - Having a problem with my first listview Itemcommand. RRS feed

  • Question

  • User-1999980185 posted

    Please review the below Code, HTML, and Error message.  I got the example from a link and I am having a problem.

    HTML Below:
    
     <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand" CommandArgument="<%# Container.DataItemIndex %>">
    
    Code Below:
    
     protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                // FindConrol code goes here
                Label LblDDate = ListView1.Item[e.commandArgument].findControl("DDate");   // <==== Error on this line
    
                // Determine what button was selected
                if (string.Equals(e.CommandName, "Select_Sel"))     // transfer to change page
    
                {
    
                    Response.Redirect("form2_Chg.aspx");
    
                }
                                   
            }
    
    
    Error Below:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'FirstCWeb_PageOne' does not contain a definition for 'DataItemIndex' and no extension method 'DataItemIndex' accepting a first argument of type 'FirstCWeb_PageOne' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx	30	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListView' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListViewCommandEventArgs' does not contain a definition for 'commandArgument' and no extension method 'commandArgument' accepting a first argument of type 'ListViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active
    

    Thursday, July 19, 2018 3:59 PM

All replies

  • User283571144 posted

    Hi db2tech@yahoo.com,

    Error	CS1061	'FirstCWeb_PageOne' does not contain a definition for 'DataItemIndex' and no extension method 'DataItemIndex' accepting a first argument of type 'FirstCWeb_PageOne' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx	30	Active
    

    According to your description and error message, I found you add the CommandArgument directly in the listview control and bind the DataItemIndex in it.

    Since the Container.DataItemIndex need inside ItemTemplate not directly in a listview.

    So you faced this error.

    I suggest you could set the property in a button control in listview’s  <ItemTemplate> section , so when you click the button , the event will be fired.

     

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListView' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListViewCommandEventArgs' does not contain a definition for 'commandArgument' and no extension method 'commandArgument' accepting a first argument of type 'ListViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active

    As far as I know, the ListView only has a definition for Items and your commandArgument  should be CommandArgument whose “C” is  upper-case.

    More details, you could refer to below codes:

    <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand" SelectMethod="GetSuppliers" ItemType="WebFormCases.Models.Supplier" >
     
                <ItemTemplate>
     
                    <tr> 
     
                        <td>  <asp:Label ID="DDate" runat="server" Text="<%#Item.SID %>"></asp:Label></td>
                             <td>  <asp:Label ID="Label2" runat="server" Text="<%#Item.Name %>"></asp:Label></td>
                             <td>  <asp:Label ID="Label3" runat="server" Text="<%#Item.City %>"></asp:Label></td>
                        <td>  
                        <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument="<%#Container.DataItemIndex %>" CommandName="Select_Sel" /></td>
                    </tr>
     
                </ItemTemplate>
     
                <LayoutTemplate>
        
                            
                                        <table id="itemPlaceholderContainer" runat="server" style="width:100%">
                                            <tr id="itemPlaceholder"></tr>
                                        </table>
                              
                      
     
                </LayoutTemplate>
     
            </asp:ListView>
     
    

    Code-behind:

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
      Label LblDDate 
    //you should convert object to int32 to get the item  and convert the finded control to Label
    =ListView1.Items[Convert.ToInt32(e.CommandArgument)].FindControl("DDate") as Label ;    
                if (string.Equals(e.CommandName, "Select_Sel"))
     
                {
     
                    Response.Redirect("form2_Chg.aspx");
     
                }
     
            }
     
            SupplyModel repo = new SupplyModel();
            public IQueryable<Supplier> GetSuppliers()
            {
                return repo.Suppliers.AsQueryable();
            }
    }
     
    

    Result:

    Best Regards,

    Brando

     

    Friday, July 20, 2018 6:12 AM
  • User-1999980185 posted

    Brando, Thanks for your help. I am new in C# and I am just learning the basic please read the below new code and new error.
    The next two statement work in VB and I would like to do the same in C#

    Dim dataitem As ListViewDataItem = CType(e.Item, ListViewDataItem)
    Dim lblddate As Label = ListView_Budget_todo.Items(dataitem.DisplayIndex).FindControl("ddate")

    This is new code below:
    
            protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                // FindConrol code goes here
                Label LblDDate = ListView1.findControl("DDate");
    
                // Determine what button was selected
                if (string.Equals(e.CommandName, "Select_Sel"))     // transfer to change page
    
                {
    
                    Response.Redirect("form2_Chg.aspx");
    
                }
    
    below is my new error:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListView' does not contain a definition for 'findControl' and no extension method 'findControl' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active
    
    ==============================================

    Friday, July 20, 2018 8:43 AM
  • User283571144 posted

    Hi db2tech@yahoo.com,

    Normally, the method in c# start with an upper-case character, so you should change “findControl” to FindControl.

    If you are not familiar with an object’s method , you could rely on visual studio’s intellisense or use shortcut key ctrl+j .

    Besides, we couldn't directly convert  ListView1.FindControl("DDate")'s result to label, since it is a control not label.

    We should firstly conver the control to lable, then we could use it.

    More details, you could refer to below codes:

                Label LblDDate = (Label)ListView1.FindControl("DDate");
    

    Best Regards,

    Brando

    Friday, July 20, 2018 9:22 AM
  • User-1999980185 posted

    What changes should I make in the new code.   This is driving me crazy.  Thanks again for all your help.

    Brando,  My button is a LINKBUTTOM not a BUTTOM do that make a difference?  Below is my new code, new markup and new error message.  Thanks for all your help.
    
    New Error Below:
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS0266	Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)	FirstCWeb	C:\Users\Edward\documents\visual studio 2015\Projects\MyFirstCWeb_Solution\FirstCWeb\FirstCWeb_PageOne.aspx.cs	131	Active
    
    New Code Below:
     protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                // FindConrol code goes here
                Label LblDDate = ListView1.FindControl("DDate");
    
                // Determine what button was selected
                if (string.Equals(e.CommandName, "Select_Sel"))     // transfer to change page
    
                {
    
                    Response.Redirect("form2_Chg.aspx");
    
                }
                                   
            }
    
    New Markup Code Below:
                <td>
                          <asp:linkbutton ID="Linkbutton1" runat="server"  text='Select' Commandname="Select_Sel" />                   
                       </td>
    
                    <td>
                          <asp:label ID="DDate" runat="server"  text='<%# Eval("DDate")%>' />                   
                      </td> 
    
    

    Friday, July 20, 2018 10:16 AM
  • User-1999980185 posted

    OK, the problem is resolved.  I had to put a as Label; in the end of the code.   Thanks for all the help.  I am getting to learn C# the hard way.  Thanks again for the help.

    Saturday, July 21, 2018 11:39 PM