【问题标题】:Unable to FindControl() in ListView ItemEditing无法在 ListView ItemEditing 中找到 FindControl()
【发布时间】:2012-05-04 05:31:12
【问题描述】:

我在 ASP.NET Web 应用程序中有一个 ListView。当用户单击编辑按钮时,我希望弹出依赖于项目某些值的文本字段。但是,我似乎在我的 ListView1_ItemEditing() 函数中找不到任何控件。

我已阅读 Internet 上的 Microsoft 文档和各种帮助主题,但他们的建议似乎对我不起作用。这通常是我看到的:

ListViewItem item = ProductsListView.Items[e.NewEditIndex];
Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");

为了简单起见,我只想能够在 ListView1_ItemEditing() 中选择一个标签。这是 ListView1_ItemEditing() 中的代码:

protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
    DataBind(); //not sure if this does anything
    ListViewItem item = ListView1.Items[e.NewEditIndex];
    Label debugLabel = (Label)item.FindControl("label_editing");
    debugLabel.Text = "Works";
}

这里是 ASP

<EditItemTemplate>
   <asp:Label ID="label_editing" runat="server" Text="hello world"></asp:Label>
</EditItemTemplate>

调试时item和debugLabel都是NULL。

更新:我通过将我的逻辑移动到 ItemDataBound 然后检查我的 tr(包含文本框)是否在该特定数据项中解决了这个问题。代码如下:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Control tr_verizon = e.Item.FindControl("tr_verizonEdit");
            Control tr_att = e.Item.FindControl("tr_attEdit");
            if (tr_verizon != null)
            {
                //Control tb_meid = e.Item.FindControl("TextBox_Meid");
                Label lbl_carrierId = (Label)e.Item.FindControl("lbl_carrierId");
                if (lbl_carrierId == null)
                {
                    Message.Text = "lbl_carrierId is null!";
                }
                else if (lbl_carrierId.Text.Equals(""))
                {
                    Message.Text = "lbl_carrierId is empty!";
                }
                else
                {
                    string recordId = lbl_carrierId.Text;

                    if (tr_verizon != null && tr_att != null)
                    {
                        if (lbl_carrierId.Text.Equals("1"))
                        {
                            tr_verizon.Visible = false;
                            tr_att.Visible = true;
                        }
                        else
                        {
                            tr_verizon.Visible = true;
                            tr_att.Visible = false;
                        }
                    }
                }
            }
        }
    }

【问题讨论】:

    标签: asp.net listview listviewitem findcontrol


    【解决方案1】:

    ItemEditing 事件在单击项目的编辑按钮时引发,但在 ListView 项目进入编辑模式之前。因此,EditItemTemplate 中的控件目前不可用。

    More Info and example

    【讨论】:

    • 好的,谢谢您的回复。您建议解决我遇到的问题的最佳方法是什么?我正在使用 SQLDataSource 从 SQL Server 获取条目列表。然后,如果用户想要编辑条目,我想根据为该行提供的数据源显示一个额外的 (具体来说,如果 IMEI 不为 NULL,我想显示一个 ATT 控件集。如果MEID 不为 NULL,我想显示一个 Verizon 控制集)。我对 ASP.NET 比较陌生,所以有时对我来说最难的事情是知道要调查什么。谢谢。
    • 没关系,另一个程序员帮我解决了这个问题。我会在 OP 中为将来可能遇到类似问题的其他人发布解决方案。
    • 这没有提供解决方案。只是一个解释。
    【解决方案2】:

    您应该先执行 DataBind(),如下所示:

        ListView1.EditIndex = e.NewEditIndex;
        ListView1_BindData(); // a function that get the DataSource and then ListView1.DataBind()
    

    // 现在像以前一样找到控件

    【讨论】:

      【解决方案3】:

      您是否尝试过强制转换 sender 对象而不是尝试按索引访问您的 ListViewItem?

      protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
      {
          var item = sender as ListViewItem;
          var debugLabel = item.FindControl("label_editing") as Label;
          debugLabel.Text = "Works";
      }
      

      【讨论】:

      • 这意味着item 为空。我想你可能想看看 Kaf 的答案。
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签