473,511 Members | 16,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best Practice Question

I have a question on whether or not this is good practice. I have a
fairly complex web user control (a datalist embedded in a datalist
with lots of controls) that I will call Usercontrol1 and a lot of code
in the codebehind of the user control for setting/getting properties
of the controls. What I would like to do is create a class like this:

public class MyClass
{
UserControl1 _userControl

public MyClass(UserControl1 userControl)
{
_userControl = userControl;
}

public void SetView()
{
_userControl.TextBox1.Text = "sometext";
Label labale1 =
(Label)_userControl.GetControl("controltofind");
etc., etc.
}
}
I want to instantiate MyClass in the UserControl1 codebehind using the
this keyword and use MyClass to set the values for my user control.

MyClass myClass = new MyClass(this);

Is this good practice and will this create any issues? I know in
theory it will work, because I have tested it (at least with one web
user, but not with multiple web users).

This would make the codebehind much more compact and readable.
Nov 22 '05 #1
4 1352
I'm not sure if this is a good typical practice, but I can tell what I would
do. I would create a base class that inherits from UserControl. All the user
controls you create must inherit from the base user control. That way, you
get to reuse your code and you will be able to deal with multiple controls
polymorphically.

============================
Hayato Iriumi (hi*****@hotmail.com)
Blog: http://www.vbaspcoder.com

"Chris" <cc****@unfi.com> wrote in message
news:49*************************@posting.google.co m...
I have a question on whether or not this is good practice. I have a
fairly complex web user control (a datalist embedded in a datalist
with lots of controls) that I will call Usercontrol1 and a lot of code
in the codebehind of the user control for setting/getting properties
of the controls. What I would like to do is create a class like this:

public class MyClass
{
UserControl1 _userControl

public MyClass(UserControl1 userControl)
{
_userControl = userControl;
}

public void SetView()
{
_userControl.TextBox1.Text = "sometext";
Label labale1 =
(Label)_userControl.GetControl("controltofind");
etc., etc.
}
}
I want to instantiate MyClass in the UserControl1 codebehind using the
this keyword and use MyClass to set the values for my user control.

MyClass myClass = new MyClass(this);

Is this good practice and will this create any issues? I know in
theory it will work, because I have tested it (at least with one web
user, but not with multiple web users).

This would make the codebehind much more compact and readable.

Nov 22 '05 #2
Thanks! This solution presents its own problems, but I think it will
work. The problem is that I have a datalist (with about 20
textboxes/dropdownlists) embedded in another datalist (with about 20
textboxes/dropdownlists). Thus, I really have to make the base class
abtsract, since I really do not want an aspx webpage associated with
it, I do not want this class to be instantiated. I will then create
the methods in the abstract class to do the formatting and then call
these in the derived class (which will have the assoicated aspx page
associated with the embeddded datalists). I had initially wanted to
create one user control for the parent list and then another for the
embedded child list, but I was not familiar with doing this and it got
to be fairly complex anyway. With your idea, by using inheritance, I
can create much cleaner code. It will not be very extensible, but
this is for a very specific project anyway so I really only need one
derived class.
Nov 22 '05 #3
Hello,
You mentioned that the solution I suggested may not be extensible. How
not extensible is it? Would you share with me?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 22 '05 #4
What I did is I created an abstract base class derived from
System.Web.UI.UserControl. I placed most of the methods for
formatting and getting the values from the controls here. Since my
control is a datalist (child)embedded in another datalist (parent), I
have to use findcontrol() a lot to access the embedded controls to get
their values. I also have to dynamically wire the events for the
child datalist and some of the other embedded controls (which there
are 40+ for the two datalists combined). Thus, there is a lot of
code. One problem I had is that I tried to create a web user control
as the base class, with the HTML and derive a class from this to see
if it would work. Unfortunately, DOTNET does not have this capability
(from what I understand, if this is possible, please let me know). So
I ended up having to create a web user control derived from my
abstract base class with the HTML elements in the derived web user
control. This works, but what I don't like about it is that the HTML
in the derived control is tied so tightly to the abstract class'
methods and vice-versa. Why I said it was not that extensible is just
the fact that unless a user had access to the HTML in my derived
control, the abstract base class would not make much sense, because
the code is so closely tied. I probably could have designed this whole
thing better, but when I began, I did not know how to do the datalist
embedded in another datalist, so I just wanted to make it work at
first.
Anyway, the end result of all this is that I now have much better
organized code (the derived class just has the events methods and a
couple of others) and a really nice user control for my project. I
just wish that I could have placed the HTML in the base class rather
than the derived class, to make the base class one self-contained
unit. That is why I said it was not that extensible, because the HTML
has to be recreated on any other derived user controls. However, that
was not my purpose here, since this control has a very specialized
role for my one project and I really just wanted cleaner code.
Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2787
by: martin | last post by:
Hi, Is there any best practice guidelines or examples for threadsafe logging? I have to write an auditlogging class for a website. The auditlog will be stored in files on the local filesystem. A...
136
9202
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
17
7999
by: | last post by:
I have an app that retrieves data from an Access database. At the moment I have the SQL string as a Const in my app. I understand this is not best practice. I don't want the user to have access to...
4
1749
by: Guy Noir | last post by:
Hello. Is there a pattern or best practice for the following scenario? I have a list of items I would like to compare. The number of items are decided at runtime. ObjectA, ObjectB,...
6
1457
by: AlexT | last post by:
Folks Please bear with me - I'm not a real ASP pro... I need some advice about the following problem: I have "inherited" of a working ASP site which is hosted on a collocated IIS machine. ...
10
2961
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
3
1802
by: cbrown | last post by:
I am rebuilding an existing application that relies on an SQL DB. The app is a scheduling/employee management program. My question pertains to best practices in dotnet and database. I use a 3...
16
2771
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
3
1975
by: Alan Isaac | last post by:
This is a simple question about actual practice. I just want to know how you (yes you) are approaching this problem. The problem: What is the recommended packaging of demo scripts or test...
5
2869
by: Frank Millman | last post by:
Hi all This is not strictly a Python question, but as I am writing in Python, and as I know there are some XML gurus on this list, I hope it is appropriate here. XML-schemas are used to...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.