【问题标题】:Multiple controls with the same ID '' were found. FindControl requires that controls have unique IDs找到具有相同 ID '' 的多个控件。 FindControl 要求控件具有唯一的 ID
【发布时间】:2011-01-25 18:58:30
【问题描述】:

更新:

这是我观察到的:

if i get    org.Registrations.Count = 1 then 
    txtBox.ID   "_registration0_0"  string
....
....

如果我得到 org.Registrations.Count = 2 那么它的行为很奇怪

txtBox.ID   "_registration2_0"  
txtBox.ID   "_registration2_1"


after that i starts again with _registration2_0

ps:如果count = 1,我没有问题

结束更新

错误消息清楚地表明我正在尝试重复 id 但下面是我创建动态文本框的地方,你看到我的代码有什么问题吗?

protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
   Students org = (namespace.Students )(e.Row.DataItem);

   foreach (Registration reg in org.Registrations)
   {
      int _count = org.Registrations.Count;
      for (int rowId = 0; rowId < _count; rowId++)
      {
         TextBox txtBox = new TextBox();
         txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId;
         txtBox.Text = reg.Name;
         e.Row.Cells[7].Controls.Add(txtBox);
     }
   }
}

【问题讨论】:

  • 您是否调试过代码,它是否为每列返回唯一 ID?我关心 Row.RowIndex ,这就是我要问的原因。调试代码并为这些设置监视值。

标签: asp.net gridview


【解决方案1】:

看来,如果您有多个 org.Registrations,您最终可能会得到重复的 ID。 看看下面测试变量的用法:

protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
   Students org = (namespace.Students )(e.Row.DataItem);
   int test =0;
   foreach (Registration reg in org.Registrations)
   {
      test++;
      int _count = org.Registrations.Count;
      for (int rowId = 0; rowId < _count; rowId++)
      {
         TextBox txtBox = new TextBox();
         txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId + "_" + test.ToString();
         txtBox.Text = reg.Name;
         e.Row.Cells[7].Controls.Add(txtBox);
     }
   }
}

【讨论】:

    【解决方案2】:

    我不需要 foreach 内的 for 循环 .. 它按预期工作...这就是为什么执行两次。

    foreach (Registration reg in org.Registrations) 
    {
       //
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-17
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      相关资源
      最近更新 更多