How do I pass a web control reference to a class?

S

Sam

Hi,

I write my code behind in C# and everytime I need a new functionality, I
build a class for it and save it in a folder I call ClassLibrary.

My question is I don't know how to send my web controls to these classes.
For example, I created a standard States and Countries class which simply
gets a list of all US states and countries in the world from the database.

This took care of getting the necessary data from the database but I still
write a lot of repetitive code because I want to bind the data to the
specific DropDownList controls I use on my ASPX page. My classes are still
pretty useful as they go fetch the data for me but I realize that I'm not
using classes to their full potential.

Someone once told me that I could pass my DropDownList control to my class
and let the class, populate it, bind it to the control and whatever else I
need to do and return it to the ASPX page all ready to be used.

I'd appreciate some simple code samples or a pointer to a web page someone
already created so that I can see how this is done. Thanks for all your
help.

Thanks,

Sam
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Sam,
This took care of getting the necessary data from the database but I still
write a lot of repetitive code because I want to bind the data to the
specific DropDownList controls I use on my ASPX page. My classes are still
pretty useful as they go fetch the data for me but I realize that I'm not
using classes to their full potential.

How do you keep the data inside that classes? if you keep it on a ArrayList
or collection then you can bind it to the ASP.net control and just call
DataBind() , Another solution would be create a strong typed collection of
State/Country and bind this collection to the control.
Someone once told me that I could pass my DropDownList control to my class
and let the class, populate it, bind it to the control and whatever else I
need to do and return it to the ASPX page all ready to be used.

And what would happen if you tomorrow decide to use these same classes for
a windows application?
You would have to write code to do the same thing. Another scenario would
be if you decide to bind the data to a control other than a DropDownList.
This imply that what you propose is not the best way, in fact the opposite
would be the correct use, create a collection of those objects in such a way
that you could use ANY control to bind them to.


Hope this help,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top