I have two aspx pages:
-
DashboardPreview.aspx: This is the homepage. It shows top 5 dashboards in a DataGridView. It shows other information too.
-
DashboardList.aspx: It shows all the dashboards with pagination in a DataGridView.
There are several controls available for each dashboard (i.e. for each row of the DataGridView). For example: a dashboard can be “edited” and “shared” with other users.
I would like to code the DataGridView only ONCE.
Then, I would like to use that code in both the pages. How do I do it?
Note: I create DataGridView by dragging and dropping it from Toolbox of Visual Studio i.e. I did not code the entire thing myself. However, I did create controls like “Share” button myself.
If you want to reuse a server control across pages then you need to create a User Control.
To create a User Control:
Your User Control will look something like this:
To use a User Control you need to register it on the page you want to use it on. The code looks like this:
Then you would use it just like a server control.
However, registering the User Control on every page can become tedious, so to make things easier you can register them in your web.config.