页面生命周期-使用FindControl引用页面加载期间以编程方式创建的控件 [英] Page Lifecycle - Using FindControl to reference a control created programmatically during page load

查看:71
本文介绍了页面生命周期-使用FindControl引用页面加载期间以编程方式创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式在窗体上创建一些文本框,以后需要使用FindControl进行引用。

I'm creating some text boxes on my form programmatically which I need to reference later using FindControl.

我已将FindControl指令放入页面加载方法中在创建它们的代码之后出现错误:

I've put the FindControl instruction in the page load method after the code which creates them but get an error:


对象引用未设置为对象的实例。

我认为这是因为文本框控件直到生命周期的后期才创建,因此无法在Page_Load中引用。

I assume this is because the textbox controls are not created until later in the lifecycle and therefore cannot be referenced from within Page_Load.

有人可以建议我在代码后面的位置放置FindControl指令,以便它可以找到这些以编程方式创建的文本框吗?

Can someone advise where in my code-behind I would need to place the FindControl instruction so that it can find these programmatically created text boxes?

推荐答案

您是否将文本框控件放置在另一个控件(例如面板或网格)中?如果是这样,则需要递归搜索页面上的所有控件。

Did you put your textbox control inside of another control such as a panel or grid? If so you need to recursively search all the controls on the page.

以下是递归FindControl实现的示例:递归Page.FindControl 。您可以通过搜索递归findcontrol找到许多其他示例。

Here is an example of a recursive FindControl implementation: Recursive Page.FindControl. You can find many other examples by googling "recursive findcontrol".

这篇关于页面生命周期-使用FindControl引用页面加载期间以编程方式创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆