Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
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 3rd, 2006, 05:56 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How count #objects of certain type, w/custom ctrl?

Hi! Thank you for reading.

I am coding my first custom control. I want the control to be as "self contained" as possible, meaning "all code must be on the custom control's class".

I want each of the "myControl" objects to know how many of the same type ("myControl") are on page. The problem I see it that only for the "final" myControl, the counting will be accurate, but I thought that a static data member could solve that...

I coded what follows

public int myControlCounter(){
    int counter=0; Type myControlType = this.GetType(), t;

            foreach (Control c in this.Page.Controls){
                t = c.GetType();
                if (t == myControlType) counter++;
    }
            return counter;
}

On the OnInit method, I would place a line like:

totalMyControls=myControlCounter(); //totalMyControls is static int

I am not sure this will work. Are there alternatives?
Am I missing something?

Thank you, for your time.
 
Old May 6th, 2006, 01:05 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: San Francisco, CA, USA.
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think a static member is the only way to do this, unless you want to do it in the parent page's class, which I don't think you want to do.

It all comes down to timing I think. When do you want your controls to know how many others are on the page? Obviously each time you increment the control counter, it will only know about the controls on the page so far.

If you do it in one of the Page's events, like maybe Page.PreRender, you can then put something like...

TotalMyControls = Page.NumberOfMyControls

into the Control.SaveViewState event handler.

Yes, then your control is dependent on the Page, but you can always overload the page class, or use a master page.

HTH

Aaron





Similar Threads
Thread Thread Starter Forum Replies Last Post
Web Service, Custom Control, Custom Return Type robzyc ASP.NET 2.0 Basics 6 June 10th, 2008 08:03 AM
how to display error msg in custom ctrl anirosek ASP.NET 1.0 and 1.1 Basics 1 July 25th, 2007 09:34 AM
Filter using ObjectDataSource and Custom Objects tna55 ASP.NET 2.0 Basics 5 July 17th, 2007 01:07 PM
Web Service Custom Objects Problem Amateur .NET Web Services 0 March 19th, 2006 11:52 AM





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