views:

220

answers:

2

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>
A: 

What method are you using "if" in?

LibraRocks
if gets called in a method that I created that gets called when the RowEditing event if fired.
Xaisoft
+1  A: 

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();
    }

  }
rick schott
I don't see an event called ItemEditing, is it the same as the RowEditing Event.
Xaisoft
Here is the list of ListView events: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.aspx
rick schott
I think are using a GridView and not a ListView
rick schott
Oops sorry, I mean to say I did not see a RowEditing event, but only a ItemEditing event. I tried your code and when it hits the part with the FindControl, it is null. I know that it is not though.
Xaisoft