繁体   English   中英

插入绑定列时FindControl在GridView上不起作用

[英]FindControl not working on GridView when inserting bound columns

我有几个项目模板的gridview。 第一个包含一个复选框,其余包含一个文本框。

然后,我动态添加了一些绑定控件,如下所示:

BoundField bdfPrivName = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfPrivName, "PrivName", "Priv Name");

BoundField bdfDescription = new BoundField();
clsUtilities.SetBoundFieldLeft(ref bdfDescription, "PrivDesc", "Description");

BoundField bdfLive = new BoundField();
clsUtilities.SetBoundFieldCenter(ref bdfLive, "Live","Active?");

grdExisting.Columns.Add(bdfPrivName);
grdExisting.Columns.Add(bdfDescription);
grdExisting.Columns.Add(bdfLive);

然后,我使用FindControl()定位复选框和文本框,并根据结果执行我的逻辑

foreach (GridViewRow gvr in grdMissing.Rows)
{ 
    mckbAny = (CheckBox)gvr.FindControl("ckbAdd");
    mtxtApplyDate = (TextBox)gvr.FindControl("txtAddApplyDate");
    mtxtDateToAdd = (TextBox)gvr.FindControl("txtAddDateToAdd");
    mtxtDateToRemove = (TextBox)gvr.FindControl("txtAddDateToRemove");
}

等等

这一切都很好。 然后,我得到一个请求,将绑定的字段放在复选框之后和文本框之前,作为第二,第三和第四列。 我发现,通过如下方式将“添加到插入”更改很容易做到:

grdExisting.Columns.Insert(1, bdfPrivName);
grdExisting.Columns.Insert(2, bdfDescription);
grdExisting.Columns.Insert(3, bdfLive);

页面看起来不错,但是FindControl()都无法正常工作。

请提出解决方案或解决方法。

提前致谢。

听起来您遇到了此错误:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104994&wa=wsignin1.0

将BoundField插入GridView时,似乎不会存储(或恢复)ViewState。 因此,当您执行FindControl时,它不存在。

您可以像以前一样尝试添加它们,并找到某种方式重新排列列(我认为这是可能的)。

我不确定它以前如何为您工作,因为控件不属于行-它们位于单元格内。 无论如何,问题在于FindControl不是递归的,它不会搜索整个控件树-仅搜索在其上运行的控件的直接子级。 您需要实现自己的递归findcontrol,例如:

public static Control FindControlRecursive(Control Root, string Id)
{
  if (Root.ID == Id)
    return Root;
  foreach (Control c in Root.Controls)
  {
    Control fc = FindControlRecursive(c, Id);
    if (fc != null)
      return fc;
  }
  return null;
}

暂无
暂无

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

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