Page.FindControl() returns null when searching for

2019-02-25 01:43发布

问题:

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");

回答1:

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