简体   繁体   English

为什么此代码无法在ListView中找到我的文本框?

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

I have a listview with an EditTemplate. 我有一个带有EditTemplate的列表视图。 If I have one item in the listview, the following code throws a null exception: 如果列表视图中有一项,则以下代码将引发null异常:

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: 这是标记的EditTemplate部分:

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

ListView.ItemEditing Event : ListView.ItemEditing事件

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

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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