0

When I try to find a control on the current page, it returns null every time and the control is right there.

HTML:

<input type="radio" id="test2" value="Hello World" name="Test"/>

Code Behind

HtmlInputRadioButton control = (HtmlInputRadioButton)Page.FindControl("test2");
            Response.Write(control.Value);
            Response.Write(control.ID);

Any idea why it keeps returning null??

enter image description here

Walters
  • 119
  • 16
  • The FindControl function is not recursive so if you have the control encapsulated in a div or table it will not find it. Try to use Master.FindControl(btnName); More info about how to locate controls when you don t know the container can be found here: https://learn.microsoft.com/en-us/previous-versions/aspnet/y81z8326(v=vs.100) – Claudio Corchez Jun 06 '19 at 17:31
  • Its not in a div but I will try master – Walters Jun 06 '19 at 17:34
  • @ClaudioCorchez Didnt work for me. – Walters Jun 06 '19 at 17:37
  • Walter you could create a function to search for the control in a recursive manner. https://blog.justincarmony.com/2008/02/12/pagefindcontrol-returning-null-issues-and-solutions-within-another-control/ – Claudio Corchez Jun 06 '19 at 17:56
  • `FindControl` finds server controls. The one you're looking for isn't a server control. No `runat="server"`. You can make it a server control (if that fits your needs.) – Scott Hannen Jun 06 '19 at 17:57
  • @ScottHannen I added runat="server" and ithe result is the same – Walters Jun 06 '19 at 19:34
  • Is it inside the form runat=server tags? It will need to be. – Scott Hannen Jun 06 '19 at 20:42
  • Yes it is. Its a weird problem. – Walters Jun 06 '19 at 20:52

0 Answers0