asp.net - Page.FindControl() 在搜索动态控件时返回 null

标签 asp.net webforms radio-button

在 asp.net web 表单中-我在 web 表单中动态添加了一组单选按钮,并在我尝试查找时单击提交按钮 使用 find.control(id) 使用其 id 的单选按钮按钮,它返回 null。有人可以帮助我是 asp.net 的新手吗?

RadioButton myControl1 = (RadioButton)Page.FindControl("r11");

最佳答案

两个原因。

当您动态添加控件时,您必须在页面回发时重新添加它们(在 Page_Init 中是个好地方)。参见 How to persist a dynamic control

另一个是Page.FindControl()只向下一层,你需要递归地向下搜索列表。参见 Better way to find control in ASP.NET

关于asp.net - Page.FindControl() 在搜索动态控件时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663763/

相关文章:

c# - 内联代码未在 OnClientClick 事件中编译

webforms - 我的 ASP.NET 4.5 WebForms 项目中这些额外的 javascript 文件是什么?

javascript - 在 Google 地球 API : Changing view with radio buttons 中

c# - 在代码隐藏中获取控制的值(value)

c# - 为什么我在 Azure 上的代码隐藏会出现 404 错误?

c# - 无法加载 configProtectedData 提供程序

jquery - 两个 "or"事件条件?

asp.net - 如何使用 ViewBag 创建 BaseController

c# - ReSharper 提示我应该在 WebForms 中执行静态方法 - 为什么?我错过了什么吗?

php - 在 html 表中使用单选按钮?