Best Practice Question

C

Chris

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.
 
H

Hayato Iriumi

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 ([email protected])
Blog: http://www.vbaspcoder.com
 
C

Chris

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.
 
H

Hayato Iriumi

Hello,
You mentioned that the solution I suggested may not be extensible. How
not extensible is it? Would you share with me?
 
C

Chris

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.
 

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