Last post Mar 16, 2017 01:32 AM by iperez_genius
Mar 15, 2017 03:24 PM|iperez_genius|LINK
I am going a little crazy...
I have a text field on the page.
<asp:TextBox ID="tbxReturnAddress" runat="server"></asp:TextBox>
and i would like to "Find the control" in codebehind like...
TextBox txtAddress = (TextBox)Page.FindControl("tbxReturnAddress");
txtAddress.Text = "asd";
The error i get is
"Object reference not set to an instance of an object."
I have put it in the page load, page pre render, inside a post back button.
Every time the same error.
Can someone advise...
Mar 15, 2017 03:40 PM|A2H|LINK
If your control is not inside any contianer control like Gridview or Datalist. You can directly access the control like below
tbxReturnAddress.Text = "Your value";
Also ensure that you are not hiding this textbox using Visible= false, is so then you need to make it visible before trying to access the control. Reason is any control which has visible= false do not render at runtime and you will face the issue you are
Mar 15, 2017 03:45 PM|iperez_genius|LINK
Mar 15, 2017 05:23 PM|PatriceSc|LINK
Really 100% sure it is not inside a container ??? What if using "view source" to check the client side id which might help to understand if it would be not just actually inside a container. Or use the debugger, or dump the full control tree.
Mar 16, 2017 01:32 AM|iperez_genius|LINK
So the issue seems to be that the find control method couldn't find what i was asking it to find.
To fix this issue i needed to wrap the controls i was asking to be found in a .net control, like a panel.
Everything worked find after that!