locked
FindControl - in ItemCommand returning a null RRS feed

  • Question

  • User-1999980185 posted

    Please see Code and Markup Below:  The statement string Ddesc = LblDdesc.Text; is returning null.  What am I doing incorrect.  See Code and Markup below

    Code Below:
    
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                // FindConrol code goes here
                Label LblDdesc = ListView1.FindControl("Ddesc") as Label;
                string Ddesc = LblDdesc.Text;                              // Error Message:  LblDdesc.Text is Null
    
                // Determine what button was selected
                if (string.Equals(e.CommandName, "Select_Sel"))     // transfer to change page
    
                {
                    Response.Redirect("form2_Chg.aspx");
                }                             
            }
    
    
    Markup Below:
    
    <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
                      
                  
      <ItemTemplate>
                    <tr style="color: #333333; ">
                      
                        <td>
                          <asp:linkbutton ID="Linkbutton1" runat="server"  text='Select' Commandname="Select_Sel" />                   
                       </td>
    
                    <td>
                          <asp:label ID="DDate" runat="server"  text='<%# Eval("DDate")%>' />                   
                      </td> 
                        
                       <td>
                          <asp:label ID="Ddesc" runat="server"  text='<%# Eval("Ddesc")%>' />                   
                      </td> 
    
                   </tr>
    
          </ItemTemplate>

    Sunday, July 22, 2018 12:17 AM

Answers

  • User-1999980185 posted

    Mudassarkhan and all that help.  I will review your code with mine and see what I am doing incorrect.   Thanks to all.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 22, 2018 11:40 PM

All replies

  • User2103319870 posted

    Instead of directly finding the controls from ListView, Try using CommandEventArgs to get the label control like below

     protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                if (String.Equals(e.CommandName, "AddToList"))
                {
                    // FindConrol code goes here
                    ListViewDataItem dataItem = (ListViewDataItem)e.Item;
                    Label LblDdesc = dataItem.FindControl("Ddesc") as Label;
                    string Ddesc = LblDdesc.Text;                              // Error Message:  LblDdesc.Text is Null
    
                    // Determine what button was selected
                    if (string.Equals(e.CommandName, "Select_Sel"))     // transfer to change page
    
                    {
                        Response.Redirect("form2_Chg.aspx");
                    }
                }
            }

    Sunday, July 22, 2018 2:23 AM
  • User-1999980185 posted

    The code below get the same error message

    ListViewDataItem dataItem = (ListViewDataItem)e.Item;
    Label LblDdesc = ListView1.FindControl("Label2") as Label;
    string Ddesc = LblDdesc.Text;

    Get same error message:   Object reference not set to an instance of an object.

    Sunday, July 22, 2018 7:01 AM
  • User-1171043462 posted

    This I have tested.

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select_Sel")
            {
                Label LblDdesc = e.Item.FindControl("Ddesc") as Label;
            }
        }

    Sunday, July 22, 2018 10:11 AM
  • User2103319870 posted

    Label LblDdesc = ListView1.FindControl("Label2") as Label;

    I think you are using wrong ID for label control. Change Label2 to "Ddesc"

    Try with below code

    ListViewDataItem dataItem = (ListViewDataItem)e.Item;
    Label LblDdesc = ListView1.FindControl("Ddesc") as Label;
    string Ddesc = LblDdesc.Text;

    Sunday, July 22, 2018 11:34 AM
  • User-1999980185 posted

    I get the same results after the change.  Are there any examples that I can view so I can determined what I am doing incorrect?

    Sunday, July 22, 2018 2:21 PM
  • User-1999980185 posted

    When I change code please see error message below with new code, and markup.  Am I missing something in my markup code?

    Code Below:
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
            {
                //     FindConrol code goes here
                //     Label LblDdesc = ListView1.[Convert.ToInt32].FindControl("Label2") as Label;
                //     Label LblDdesc = (Label)ListView1.FindControl("Label2") as Label;
    
    
                ListViewDataItem dataItem = (ListViewDataItem)e.Item;
                Label LblDdesc = ListView1.e.Item.FindControl("Ddesc") as Label;
                string Xxxx = LblDdesc.Text;   
    
    
    
    Markup Below:
    
    <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
                      
                  
      <ItemTemplate>
                    <tr style="color: #333333; ">
                      
                        <td>
                          <asp:linkbutton ID="Linkbutton1" runat="server"  text='Select' Commandname="Select_Sel" />                   
                       </td>
    
                    <td>
                          <asp:label ID="DDate" runat="server"  text='<%# Eval("DDate")%>' />                   
                      </td> 
                                        
                       <td>
                          <asp:label ID="Ddesc" runat="server"  text='<%# Eval("Ddesc")%>' />                   
                      </td> 
    
                   </tr>
    
          </ItemTemplate>
    
    
    Error Below:
    
    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1061	'ListView' does not contain a definition for 'e' and no extension method 'e' 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	136	Active
    

    Sunday, July 22, 2018 2:36 PM
  • User-1999980185 posted

    Mudassarkhan you said you tested your answer, could you please send me a copy of the markup because I think that I am doing something incorrect in my markup.  Thank you.

    Sunday, July 22, 2018 7:14 PM
  • User-1171043462 posted
     <asp:ListView ID="UsersListView" AutoPostBack="true" runat="server" OnSelectedIndexChanging="UsersListView_SelectedIndexChanging"
            OnSelectedIndexChanged="UsersListView_SelectedIndexChanged">
            <LayoutTemplate>
                <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                    <tr>
                        <td id="itemPlaceholder" runat="server">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr id="Tr1" runat="server">
                    <td>
                        <asp:LinkButton ID="UserNameLinkButton" CommandName="Select" runat="server" Text='<%# Eval("Name") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <SelectedItemTemplate>
                <tr id="Tr1" runat="server" style = "background:#336699">
                    <td>
                        <asp:LinkButton ID="UserNameLinkButton" CommandName="Select" runat="server" Text='<%#  Eval("Name") %>'
                            ForeColor="White" />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>

    Sunday, July 22, 2018 8:29 PM
  • User-1171043462 posted

    Complete Code

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("DDate"), new DataColumn("Ddesc") });
                dt.Rows.Add("1", "2");
                dt.Rows.Add("3", "4");
                ListView1.DataSource = dt;
                ListView1.DataBind();
            }
        }
        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Select_Sel")
            {
                Label LblDdesc = e.Item.FindControl("Ddesc") as Label;
            }
        }
     <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
            <ItemTemplate>
                <tr style="color: #333333;">
                    <td>
                        <asp:LinkButton ID="Linkbutton1" runat="server" Text='Select' CommandName="Select_Sel" />
                    </td>
                    <td>
                        <asp:Label ID="DDate" runat="server" Text='<%# Eval("DDate")%>' />
                    </td>
                    <td>
                        <asp:Label ID="Ddesc" runat="server" Text='<%# Eval("Ddesc")%>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>

    Sunday, July 22, 2018 8:43 PM
  • User-1999980185 posted

    Mudassarkhan and all that help.  I will review your code with mine and see what I am doing incorrect.   Thanks to all.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 22, 2018 11:40 PM
  • User-1171043462 posted

    Can you please mark my reply too that helped as Answer for others to know which reply helped?

    Monday, July 23, 2018 7:50 AM