为什么Controls可以找到控件,而FindControl找不到

niuniuhuang 2009-03-12 12:53:08
代码如下,就一个测试页面,里面就一个日历Calendar1,后台就一个Calendar1的OnDayRender事件
在事件里面给e.Cell加了一个Labal用foreach (Control cControl in e.Cell.Controls)可以遍历刚刚加的Label,
但用e.Cell.FindControl("lbl")却找不到,为什么?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender"></asp:Calendar>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
Label lbl = new Label();
lbl.ID = "lbl";
e.Cell.Controls.Add(lbl);
foreach (Control cControl in e.Cell.Controls)
{
Response.Write(cControl.ID);
}
if (e.Cell.FindControl("lbl") != null)
{
Response.Write("找到了");
}
}
}
...全文
754 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
cwmwss 2009-03-17
  • 打赏
  • 举报
回复
呵呵
time_is_life 2009-03-12
  • 打赏
  • 举报
回复
e.Cell.Controls.FindControl("lbl")
niuniuhuang 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jinjazz 的回复:]
因为e.Cell没有实现接口INamingContainer
[/Quote]
谢谢,可能是这样吧
但我搞不懂微软为什么这样做,既然我可以往里加控件,而且还可以用e.Cell.Controls遍历出来,
那为什么就不能用e.Cell.FindControl找到控件,
不给我找到,就别给我这个方法咯,给了方法又找不到,真郁闷
way106vip 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jinjazz 的回复:]
因为e.Cell没有实现接口INamingContainer
[/Quote]
正解
time_is_life 2009-03-12
  • 打赏
  • 举报
回复
TO jinjazz
你是正确的,牛人!
jinjazz 2009-03-12
  • 打赏
  • 举报
回复
这是FindControl的实现代码,看一下就知道了

public virtual Control FindControl(string id)
{
return this.FindControl(id, 0);
}
protected virtual Control FindControl(string id, int pathOffset)
{
string str;
this.EnsureChildControls();
if (!this.flags[0x80])
{
Control namingContainer = this.NamingContainer;
if (namingContainer != null)
{
return namingContainer.FindControl(id, pathOffset);
}
return null;
}
if (this.HasControls() && (this._occasionalFields.NamedControls == null))
{
this.EnsureNamedControlsTable();
}
if ((this._occasionalFields == null) || (this._occasionalFields.NamedControls == null))
{
return null;
}
char[] anyOf = new char[] { '$', ':' };
int num = id.IndexOfAny(anyOf, pathOffset);
if (num == -1)
{
str = id.Substring(pathOffset);
return (this._occasionalFields.NamedControls[str] as Control);
}
str = id.Substring(pathOffset, num - pathOffset);
Control control2 = this._occasionalFields.NamedControls[str] as Control;
if (control2 == null)
{
return null;
}
return control2.FindControl(id, num + 1);
}

jinjazz 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 time_is_life 的回复:]
e.Cell是个TableCell
TableCell是有FindControl这个方法的,而且微软有官方的例子:
http://msdn.microsoft.com/en-us/library/486wc64h.aspx

所以我怀疑是你加的控件被加到一个子控件中去了,而FindControl只搜索一层。 试试
e.Cell.Controls[0].FindControl
或者用递归查找。
[/Quote]

他是从Control继承下来了,没有实现INamingContainer 的是不会被找的

Searches the current naming container for a server control with the specified id parameter.

这是你的链接中的第一句话..
zzxap 2009-03-12
  • 打赏
  • 举报
回复
if (this.FindControl("lbl") != null)
{
Response.Write("找到了");
}
time_is_life 2009-03-12
  • 打赏
  • 举报
回复
e.Cell是个TableCell
TableCell是有FindControl这个方法的,而且微软有官方的例子:
http://msdn.microsoft.com/en-us/library/486wc64h.aspx

所以我怀疑是你加的控件被加到一个子控件中去了,而FindControl只搜索一层。 试试
e.Cell.Controls[0].FindControl
或者用递归查找。
zzxap 2009-03-12
  • 打赏
  • 举报
回复


if ((label)this.FindControl(("lbl") != null)
{
Response.Write("找到了");
}
Cherishny 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jinjazz 的回复:]
因为e.Cell没有实现接口INamingContainer
[/Quote]
学习
zzxap 2009-03-12
  • 打赏
  • 举报
回复

if (e.Cell.Controls.FindControl("lbl") != null)
{
Response.Write("找到了");
}
liudanking 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jinjazz 的回复:]
因为e.Cell没有实现接口INamingContainer
[/Quote]
答案已经出来了。
xingn 2009-03-12
  • 打赏
  • 举报
回复
2楼正解
jinjazz 2009-03-12
  • 打赏
  • 举报
回复
试试
this.NamingContainer.FindControl
ZZJ_4Ever 2009-03-12
  • 打赏
  • 举报
回复
正解
CutBug 2009-03-12
  • 打赏
  • 举报
回复
if (e.Cell.Controls.Contains(lbl))
{
Response.Write("找到了");
}
jinjazz 2009-03-12
  • 打赏
  • 举报
回复
因为e.Cell没有实现接口INamingContainer

62,055

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧