vcbertini
asked on
Having trouble with FindControl
I have an application where I need to perform operations on various controls throughout the page based on my repeater data. I need to populate Label Text, set Visible to true/false, etc.
The basic structure (framework) of my page is as such:
Repeater 1
>> Label 1
>> Literal 1
>> Nested Repeater
>> >> Label 2
>> >> Hyperlink 2
>> >> >> Nested Nested Repeater
>> >> >> Hyperlink 3
>> >> >> END nested Repeater
>> END nested Repeater
END Repeater1
I am having an impossible time knowing
(1) When to check for the control (i.e. what can I do during Page_Load, or DataBind, etc), and
(2) the syntax of the control (i.e. (Label)Page.FindControl("c ontroName" ); OR (Label)This.FindControl("c ontrolName ") OR (Label)e.Item.FindControl( "controlNa me")
The error I keep getting is "Object reference not set to an instance of an object"
Can anyone clear this up for me?
The basic structure (framework) of my page is as such:
Repeater 1
>> Label 1
>> Literal 1
>> Nested Repeater
>> >> Label 2
>> >> Hyperlink 2
>> >> >> Nested Nested Repeater
>> >> >> Hyperlink 3
>> >> >> END nested Repeater
>> END nested Repeater
END Repeater1
I am having an impossible time knowing
(1) When to check for the control (i.e. what can I do during Page_Load, or DataBind, etc), and
(2) the syntax of the control (i.e. (Label)Page.FindControl("c
The error I keep getting is "Object reference not set to an instance of an object"
Can anyone clear this up for me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I always have the same problem. if you don't want to do the recursive thing then start in the Page_Load, and use this to get the top level control.
Control pageControl = ((Control)sender).Parent;
Then using the debugger you can work your way down to the control you are interested in. Once you find it, in the debugger, you will then know how to code your way down to it. In your code, check for null at each step to avoid the "object reference not set" problem.
Control pageControl = ((Control)sender).Parent;
Then using the debugger you can work your way down to the control you are interested in. Once you find it, in the debugger, you will then know how to code your way down to it. In your code, check for null at each step to avoid the "object reference not set" problem.
ASKER
That seems to work - took an example of the code off of a different post.
And if you want to find controls inside the repeater, page.findcontrol() will not help. e.item.findcontrol() is a good one, but still, life will be easier if you can provide source code.
Generally, this article is a very good one concerning "FindControl" in asp.net
http://odetocode.com/Articles/116.aspx