Page.FindControl() 搜索动态控件时返回 null [英] Page.FindControl() returns null when searching for dynamic control

查看:22
本文介绍了Page.FindControl() 搜索动态控件时返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

两个原因.

动态添加控件时,必须在页面回发时重新添加(在Page_Init中是个好地方).请参阅如何持久化动态控件

另一个是 Page.FindControl() 只向下一级,你需要递归地向下搜索列表.请参阅在 ASP.NET 中查找控件的更好方法>

In asp.net web form-I have added group of radio button dynamically in web form and on clicking submit button when i try to the find the radio button button with its id using find.control(id), it returns null. Can some body help me am new to asp.net?

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

解决方案

Two reasons.

When you add controls dynamically, you must re-add them when the page posts back (In Page_Init is a good place). See How to persist a dynamic control

The other is that Page.FindControl() only goes one level down, you need to recursively search down the list. See Better way to find control in ASP.NET

这篇关于Page.FindControl() 搜索动态控件时返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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