I have a complex asp.net form,having even 50 to 60 fields in one form like there is Multiview
, inside MultiView I have a GridView
, and inside GridView I have several CheckBoxes
.
Currently I am using chaining of the FindControl()
method and retrieving the child ID.
Now, my question is that is there any other way/solution to find the nested control in ASP.NET.
FindControl does not search within nested controls recursively. It does only find controls that's NamigContainer is the Control on that you are calling FindControl.
Theres a reason that ASP.Net does not look into your nested controls recursively by default:
Consider you want to encapsulate your GridViews, Formviews, UserControls etc. inside of other UserControls for reusability reasons. If you would have implemented all logic in your page and accessed these controls with recursive loops, it'll very difficult to refactor that. If you have implemented your logic and access methods via the event-handlers(f.e. RowDataBound of GridView), it'll be much simpler and less error-prone.
Recursively find all controls matching the specified predicate (do not include root Control):
Usage:
All the highlighted solutions are using recursion (which is performance costly). Here is cleaner way without recursion:
I decided to just build controls dictionaries. Harder to maintain, might run faster than the recursive FindControl().
And before I get down-thumbs for not answering the OP's question...
Q: Now, my question is that is there any other way/solution to find the nested control in ASP.NET? A: Yes, avoid the need to search for them in the first place. Why search for things you already know are there? Better to build a system allowing reference of known objects.
The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. If TextBox2 is not found, "Control Not Found" is written to the page.
Action Management On Controls
Create below class in base class. Class To get all controls:
From Database: Get All Actions IDs (like divAction1,divAction2 ....) dynamic in DATASET (DTActions) allow on specific User.
In Aspx: in HTML Put Action(button,anchor etc) in div or span and give them id like
IN CS: Use this function on your page: