Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2006, 02:47 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Custom Control with Multiple Constructors?

Hi! Thank you for reading.
I am just starting with Custom Web Controls.

I developed 2 constructors for my first custom control.
My problem is that ONLY the "default"/no arguments constructor gets called.
I think this happens because I do NOT know how to build my control, the right way, in the ASPX:

For example, when I want to pass arguments p1 and p2, I write what follows in the ASPX and it works, as far as passing the values to public properties named p1 and p2, but won't call a two parameter constructor.

<prefix:class p1="1" p2="2"></prefix:class>

So, my questions are:
#1) can I have multiple constructors with custom web controls?
#2) if yes, then what should I write in the ASPX, to call a specific constructor?

Thank you, for your time.
 
Old May 1st, 2006, 03:28 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My understanding was that you don't even create your own constructors in ASP.NET, unlike other .NET applications. I was under the impression that objects were sort of automatically created and destroyed according to the Page/Control lifecycle, which is why you never see constructors in the code, just event handlers. I could be wrong about this.

Aaron

 
Old May 1st, 2006, 03:40 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, for your reply!

The default constructor does get called. I am sure about this, because I change some relevant values inside it, and those changes do stick.

I don't know if this is good practice (to use a constructor for the custom control), or not, but that is not the point.

As for the other constructors, the questions remain: #1) can they be called; #2) if yes, how?
 
Old May 1st, 2006, 03:55 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The constructor does get called of course when the object is initialized. What I thought was that we don't have any control over that constructor and what it does. It sounds like I was mistaken though, because you wrote some code that changes some values inside of it. Did you do this inside an event handler then (I've never seen a constructor in the classic object-oriented vein in ASP.NET)?

So what you are asking is can you overload the constructor then. And my understanding is that because the server control objects are automatically created one at a time and added to the page object (through event handlers, i.e. in the ControlInit event handler), that no, you can't.

Again, I could be wrong about this. Maybe someone else has an idea. What I always do is expose properties on the custom control that, when set, execute some code. This code will then get executed regardless of where the property is changed, i.e. in the control's declaration, or later on in code.

HTH

Aaron

 
Old May 1st, 2006, 04:03 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, no event handler. I am really starting...

It is as basic as this:

class myCustomControl{
private int mP1; private int mP2;
//aP1 and aP2 are public properties for mP1 and mP2, respectively
public myCustomControl(){mP1=aP1; mP2=aP2;}
public myCustomControl(int p1, int p2){mP1=p1; mP2=p2;}
...
}//class ends

The 2nd constructor is an overload that never gets called.
 
Old May 1st, 2006, 04:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Although I could be wrong, I don't think there's a way to do this.

After all, how would the runtime know what constructor to call? All you provide is a control tag and a number of attributes that map to properties. The only thing that the runtime can rely on (or at least make an intelligent guess about) is a default constructor. You'll get an error when you remove it from your control's code.

I think if you look at the MSIL of other ASP.NET controls, you'll find they all have at least a parameterless default constructor.

Some other areas in .NET allow you to supply your own instance of an object, so you can call an overloaded version of your constructor. The ObjectDataSource is a nice example of that. Inside its ObjectCreating event you can assign a manually created object.

Is there any reason why you need a complex constructor? Why not use the default constructor, accept the parameters that .NET sets to the properties for you, and then let your control do what it needs to do in the various events of the control's life cycle?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 1st, 2006, 05:37 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Right now I am just trying to understand if it is possible to do it - it seems it is not.

I am maturing an idea that might require multiple constructors, but it is a work in progress and my current (very poor) knowledge can't produce an answer, regarding if I can work around the constructors limitation, or not - I probably can.

 
Old May 1st, 2006, 06:06 PM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is one situation where, I think, multiple constructors are required:

one object is created with a certain "energy";
his "energy" will be increased/decreased on runtime, *only* according to certain events, like every two minutes a decrease, and every click an increase...

I would like to say the "energy" on construction time, but NOT allow it to change, via some property, so I thought about 2 constructors: #1) the caller says nothing and the object gets, for example, an initial energy of 50;
#2) the caller says how much initial energy wants, and can't change it directly...

Aren't multiple constructors really mandatory for this?

 
Old May 1st, 2006, 06:15 PM
Friend of Wrox
 
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

If you're experience is with an object oriented language such as C++, then ASP.NET is going to seem a little weird to you. The runtime automatically takes care of object construction, copy construction, destruction, operator overloads etc. Although apparently you can instantiate user controls in code at runtime (something I haven't had the need to do yet), you usually do it delaratively in the ASPX file. The framework takes care of all the dirty work, and allows you to modify the results with event handlers.

Now this is not the case with Windows, console and other applications develped in .NET, just in ASP. Remember that in ASP you are working against a stateless environment and a whole bunch of complication results from the need to maintain state.

My suggestion to you is to try what I talked about earlier. Create public properties in your user controls that, when set, do something. Here's an example from one of my sites (http:www.heatherlauren.com). This comes from an ASCX control file and just changes the navigation display on the left of the page.

Partial Class HeatherNavs
    Inherits System.Web.UI.UserControl
    Private Selected_ As String
    Public Property Selected() As String

        Get
            Return Selected_
        End Get
        Set(ByVal value As String)
            Selected_ = value
            Dim Circle As Image
            Circle = Me.FindControl(value & "_Left_Circle")
            Circle.ImageUrl = "images/ClosedCircle.jpg"
            Circle = Me.FindControl(value & "_Right_Circle")
            Circle.ImageUrl = "images/ClosedCircle.jpg"

        End Set
    End Property
End Class


Hope this helps.

Aaron

 
Old May 2nd, 2006, 12:07 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi three,

I understand your reasoning. For such a scenario, an overloaded constructor that accepts two values, and two read-only properties makes a lot of sense.

You can recreate this behavior, though. Internally, keep track of either a default value, or whether the property has been set. When the value is set again, simply ignore it if the backing value does not equal the default value.....

Aaron is right when he says you can create controls dynamically. You could do something like this:

Dim myControl As SomeControl = New SomeControl(10, 20)

However, this control does not participate in the ASP.NET page event life cycle. Instead you need this:

Dim myControl As SomeControl = CType(LoadControl("PathToControl"), SomeControl)

This creates a control that participates in events like Load etc. However, with this way of creating controls, you again loose the ability to use an overloaded constructor.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Server Control....Custom Property Editor ZArrinPour ASP.NET 1.0 and 1.1 Basics 1 June 15th, 2010 11:30 AM
Web Service, Custom Control, Custom Return Type robzyc ASP.NET 2.0 Basics 6 June 10th, 2008 08:03 AM
custom control inside custom control issues StevesonD ASP.NET 2.0 Professional 1 February 19th, 2008 06:54 PM
multiple constructors for custom controls three BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 0 May 1st, 2006 03:24 PM
Multiple Constructors Page 194 NUTSHELL BOOK: Beginning Java 2 2 February 5th, 2004 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.