Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Use User Controls If I'm Not Going To Reuse The Code?

First of all, I hope I'd get some advice about my practice because based on the very few books I've read, they didn't write much in the aspx page..they just built some controls and used them in the aspx page, so is this approach a good practice ?

here comes my question, I thought using web controls instead of directly writing into .aspx page is better as I could reuse the code, but now I'm creating those controls and I don't think I'll reuse them again or maybe only just one more time. so do you think it's wise to create a control for the code instead of directly coding in the .aspx page ?

I was also working on a web user control for adding a new item to my db, and then I started planing for the update or edit control..I thought maybe I'd use the same control for both add and edit and start reusing my code, and on my way editing the control to be able to function as both add and edit control, I started with adding properties to the control, then a couple assignments in the Load method, then some checks with if...So I realized maybe a new control would be better!

I don't know, I'm thinking intuitively but I could really use a professional, experienced point of view.

Thanks for your time =)

like image 223
Mazen Elkashef Avatar asked Dec 16 '10 02:12

Mazen Elkashef


3 Answers

Sometimes creating a user control, allows you to encapsulate some specific logic and ui elements into a separate class. Even if you are not reusing the control, the final code may be simpler to read and maintain. Take by example a Login control, if you take login related decisions in the user control and make those 'details' hidden in the rest of your code, then the code get simpler and easier to read and mantain!

like image 189
Gerardo Grignoli Avatar answered Nov 15 '22 04:11

Gerardo Grignoli


If you're not going to reuse the code, then you don't want a user control or any other kind of control. Just put the appropriate code and controls onto the page.

If you find later that you do want to reuse it, then you can make a user control out of it.

like image 29
John Saunders Avatar answered Nov 15 '22 04:11

John Saunders


If you know for sure that you are going to want to use a control (or some slight variation) then creating the user control is a no brainer.

For me, if it occurs to me that I may need similar functionality again in some future project then I will sometimes create a control just because I think it will be useful.

like image 26
wcm Avatar answered Nov 15 '22 05:11

wcm