简体   繁体   中英

Why isn't this code finding my textbox in my ListView?

I have a listview with an EditTemplate. If I have one item in the listview, the following code throws a null exception:

if((TextBox)this.lvwColors.EditItem.FindControl("txtColor")) != null)
{
   this.Color =    
   ((TextBox)this.lvwColors.EditItem.FindControl("txtColor")).Text.Trim();
}

It does not throw an error if I have two items in my listview.

The other strange thing is that it only throws an error when on a production server, but when I am testing locally, I do not get the error. Does anyone know why this behavior might exist?

Here is EditTemplate part of the markup:

<EditItemTemplate>          
    <asp:TextBox ID="txtColor" runat="server" 
                 Text='<%#   Eval("Color").ToString().Trim() %>'
</EditItemTemplate>

ListView.ItemEditing Event :

ListView:

 protected void yourListView_ItemEditing(Object sender, ListViewEditEventArgs e)
  {
    ListViewItem item = yourListView.Items[e.NewEditIndex];

    if((TextBox)item.FindControl("txtColor") != null)
    {
       this.Color =    
          ((TextBox)item.FindControl("txtColor")).Text.Trim();
    }

  }

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