Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a custom user control, and in it is a gridview. I need to perform a database query and bind data to that gridview based on what is on the page I loaded the control onto. I'm dynamically creating the controls, so I'm wondering if I can overload the constructor or something? Or do I have to call a postback or something? The user controls are being instantiated in an overloaded OnInit() method on the page. Thanks!
Posted
Updated 24-Mar-13 16:44pm
v2

1 solution

If you pass some data to an instance of some class and use it somewhere beyond just this passing method, it cannot be a "variable"; it has to be a class member, a field, a property, or both (property backed by a field).

Yes, you can add a constructor with appropriate parameters, but this will allow you to modify some value of some (instance) member(s) only once. And this is not the intended approach for the design of use control classes.

The "standard" design is using parameterless constructors only. To solve your problem, you need to add an internal or (more usually) public read-write property. Note that using an internal or public field is considered a bad style, by some good reasons.

—SA
 
Share this answer
 
Comments
Jack R. Schaible 25-Mar-13 0:23am    
I understand that. My issue is, if I instantiate that class, and set the property (either just by dot accessor or by a method), and then databind the gridview inside it, would I not have to somehow do I postback for the gridview to show the data I need to pull?
Sergey Alexandrovich Kryukov 25-Mar-13 0:33am    
There is no a single answer on the use of it. It depends on what do you need to achieve...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900