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

发布于 2024-09-30 20:49:39 字数 1292 浏览 3 评论 0原文

我有一个带有几个 ItemTemplates 的网格视图。第一个包含一个复选框,其余包含文本框。

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

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(),所有这些都无法工作。

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

提前致谢。

I have a gridview with several ItemTemplates. The first contains a checkbox the rest contain textboxes.

I then added dynamically some bound controls like this:

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

I then use FindControl() to locate the checkbox and textboxes and perform my logic based the result

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

etc.

This all worked fine. I then got a request to put the bound fields as the second, third and fourth columns, after the check box and before the textboxes. I found that this was easy to do by changing the Add’s to Inserts as follows:

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

It looked fine of the page, but the FindControl(), all of them fail to work.

Please suggest a solution or a workaround.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感悟人生的甜 2024-10-07 20:49:39

听起来您遇到了这个错误:

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

当 BoundField 插入 GridView 时,ViewState 似乎未存储(或恢复)。所以当你执行 FindControl 时它并不存在。

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

It sounds like you have come across this bug:

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

It appears ViewState is not stored (or restored) when a BoundField is inserted into a GridView. So when you do FindControl it doesn't exist.

You could try adding them as you did before and finding some way of re-arranging the columns (I think this is possible).

庆幸我还是我 2024-10-07 20:49:39

我不确定它之前是如何为您工作的,因为控件不属于行 - 它们位于单元格内。无论如何,问题是 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;
}

I am not sure how it was working for you before, as controls don't belong in row - they are inside cells. Anyways, the issue is that FindControl is not recursive, it will not search entire control tree - only immediate children of the control you run it on. You need to implement your own recursive findcontrol, for example like so:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文