473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to creating Server Controls. Have some questions.

I am just starting the process of creating ASP.NET server controls. I have
created controls for .NET applications, but have just started with ASP.NET.
I am a little confused about some areas that I am hoping someone can help
clear up.

1. What is the difference between initializing a control in the
constructor, vs the OnInit(), vs the CreateChildCont rols() methods?
2. The control that I created contains an Items collection attribute that I
implemented. I am using the base WebControl class for my control and
constructed it using the DIV type. I noticed that the Items attribute was
rendered to the web page as a Items="(Collect ion)" attribute. Why and how
does the base class know to add this to the attribute of the <DIV> tag?
What is the significance of setting this attribute since the state of the
data is not preserved?
3. I implemented the data postback interface and registered it using
RegisterRequire sPostback(). When using the debugger, I notice that the
LoadPostData method is called prior to calling the OnInit() method. Why is
this? I would expect the OnInit() method be called first?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
Nov 18 '05 #1
3 1857
Hi Ken,

1. You should always create your controls in the CreateChildCont rols method per best practices. If for some reason you need to access control properties prior to the OnInit method (which is possible), you're controls need to be there, and as such...if you call this.EnsureChil dControls(), it will make sure that CreateChildCont rols is called. An example of this is if you're creating a composite control, and you need to access child properties of a control prior to OnInit being called. In your get property, you do a EnsureChildCont rols(), and then reference the control's property.

2. You shouldn't be seeing this in your HTML. There are certain attributes you need to apply to your collection's property that will make it do what you wish. Here's an example:

[Bindable(false) ,
Browsable(false ),
Category("Appea rance"),
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Hidden)]
public virtual HolidayCollecti on Holidays { ... }

If you're creating a custom collection, that collection will need to store its viewstate, and you'll need to manually store/load viewstate for that collection using the LoadViewState and SaveViewState methods.

3. Going back to my comment for 1, you should never use OnInit unless you really, really needed to. You should use CreateChildCont rols. The reason it is calling prior to that, is because its loading the post data, which you need prior to LoadViewState so the correct data is represented in the control. If OnInit were called before hand, you would have "old" data when trying to work with it. Do you understand?

Matt Hawley, MCAD .NET
http://www.eworldui.net

I am just starting the process of creating ASP.NET server controls. I have
created controls for .NET applications, but have just started with ASP.NET.
I am a little confused about some areas that I am hoping someone can help
clear up.

1. What is the difference between initializing a control in the
constructor, vs the OnInit(), vs the CreateChildCont rols() methods?
2. The control that I created contains an Items collection attribute that I
implemented. I am using the base WebControl class for my control and
constructed it using the DIV type. I noticed that the Items attribute was
rendered to the web page as a Items="(Collect ion)" attribute. Why and how
does the base class know to add this to the attribute of the <DIV> tag?
What is the significance of setting this attribute since the state of the
data is not preserved?
3. I implemented the data postback interface and registered it using
RegisterRequire sPostback(). When using the debugger, I notice that the
LoadPostData method is called prior to calling the OnInit() method. Why is
this? I would expect the OnInit() method be called first?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 18 '05 #2
So, it sounds like the class constructor on a server control should not be
used for any initialization in ASP.NET?

Also, in regards to #3, I am still a little confused. I am assuming that
all class data objects must be reconstructed somehow on the postback. If
that includes instantiating objects that are part of that class, then
wouldn't that have to be done in the OnInit() call first? Otherwise, the
class member objects would be invalid when trying to supply data to them in
the LoadPostData() method. So, I am not sure where I should be newing my
class objects. Should I call EnsureChildCont rols() in my LoadPostData()
method?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Matt Hawley" <mhawley@n!o!s! p!a!m.integrity ts.com> wrote in message
news:O3******** ********@tk2msf tngp13.phx.gbl. ..
Hi Ken,

1. You should always create your controls in the CreateChildCont rols method per best practices. If for some reason you need to access control
properties prior to the OnInit method (which is possible), you're controls
need to be there, and as such...if you call this.EnsureChil dControls(), it
will make sure that CreateChildCont rols is called. An example of this is if
you're creating a composite control, and you need to access child properties
of a control prior to OnInit being called. In your get property, you do a
EnsureChildCont rols(), and then reference the control's property.
2. You shouldn't be seeing this in your HTML. There are certain attributes you need to apply to your collection's property that will make it
do what you wish. Here's an example:
[Bindable(false) ,
Browsable(false ),
Category("Appea rance"),
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Hidden)] public virtual HolidayCollecti on Holidays { ... }

If you're creating a custom collection, that collection will need to store its viewstate, and you'll need to manually store/load viewstate for
that collection using the LoadViewState and SaveViewState methods.
3. Going back to my comment for 1, you should never use OnInit unless you really, really needed to. You should use CreateChildCont rols. The reason
it is calling prior to that, is because its loading the post data, which you
need prior to LoadViewState so the correct data is represented in the
control. If OnInit were called before hand, you would have "old" data when
trying to work with it. Do you understand?
Matt Hawley, MCAD .NET
http://www.eworldui.net

I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help
clear up.

1. What is the difference between initializing a control in the
constructor, vs the OnInit(), vs the CreateChildCont rols() methods?
2. The control that I created contains an Items collection attribute that I implemented. I am using the base WebControl class for my control and
constructed it using the DIV type. I noticed that the Items attribute was
rendered to the web page as a Items="(Collect ion)" attribute. Why and how
does the base class know to add this to the attribute of the <DIV> tag?
What is the significance of setting this attribute since the state of the
data is not preserved?
3. I implemented the data postback interface and registered it using
RegisterRequire sPostback(). When using the debugger, I notice that the
LoadPostData method is called prior to calling the OnInit() method. Why is this? I would expect the OnInit() method be called first?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Nov 18 '05 #3
Ken,

Thats right, you should avoid using a constructor or OnInit method to instantiate your controls.

I cant argue why MS did it this way, its just the way it is...and it was probably due to how most developers would code. I know I would hate getting "old" data in my OnInit (or the Init event) method, because it wouldn't be viable data I could program against. That is the reason it was done in that manner.

It wouldn't hurt calling EnsureChildCont rols() in your LoadPostData, this will ensure that they've been created and added in the same manner prior to a postback. Note, if the CreateChildCont rols has previously been called (either by you, or the framework), successively calling EnsureChildCont rols will not call CreateChildCont rols.

Matt Hawley, MCAD .NET
http://www.eworldui.net

So, it sounds like the class constructor on a server control should not be
used for any initialization in ASP.NET?

Also, in regards to #3, I am still a little confused. I am assuming that
all class data objects must be reconstructed somehow on the postback. If
that includes instantiating objects that are part of that class, then
wouldn't that have to be done in the OnInit() call first? Otherwise, the
class member objects would be invalid when trying to supply data to them in
the LoadPostData() method. So, I am not sure where I should be newing my
class objects. Should I call EnsureChildCont rols() in my LoadPostData()
method?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Matt Hawley" <mhawley@n!o!s! p!a!m.integrity ts.com> wrote in message
news:O3******** ********@tk2msf tngp13.phx.gbl. ..
Hi Ken,

1. You should always create your controls in the CreateChildCont rols method per best practices. If for some reason you need to access control
properties prior to the OnInit method (which is possible), you're controls
need to be there, and as such...if you call this.EnsureChil dControls(), it
will make sure that CreateChildCont rols is called. An example of this is if
you're creating a composite control, and you need to access child properties
of a control prior to OnInit being called. In your get property, you do a
EnsureChildCont rols(), and then reference the control's property.
2. You shouldn't be seeing this in your HTML. There are certain attributes you need to apply to your collection's property that will make it
do what you wish. Here's an example:
[Bindable(false) ,
Browsable(false ),
Category("Appea rance"),
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Hidden)] public virtual HolidayCollecti on Holidays { ... }

If you're creating a custom collection, that collection will need to store its viewstate, and you'll need to manually store/load viewstate for
that collection using the LoadViewState and SaveViewState methods.
3. Going back to my comment for 1, you should never use OnInit unless you really, really needed to. You should use CreateChildCont rols. The reason
it is calling prior to that, is because its loading the post data, which you
need prior to LoadViewState so the correct data is represented in the
control. If OnInit were called before hand, you would have "old" data when
trying to work with it. Do you understand?
Matt Hawley, MCAD .NET
http://www.eworldui.net

I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help
clear up.

1. What is the difference between initializing a control in the
constructor, vs the OnInit(), vs the CreateChildCont rols() methods?
2. The control that I created contains an Items collection attribute that I implemented. I am using the base WebControl class for my control and
constructed it using the DIV type. I noticed that the Items attribute was
rendered to the web page as a Items="(Collect ion)" attribute. Why and how
does the base class know to add this to the attribute of the <DIV> tag?
What is the significance of setting this attribute since the state of the
data is not preserved?
3. I implemented the data postback interface and registered it using
RegisterRequire sPostback(). When using the debugger, I notice that the
LoadPostData method is called prior to calling the OnInit() method. Why is this? I would expect the OnInit() method be called first?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


Nov 18 '05 #4

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

Similar topics

2
8387
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
3
1046
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
22
2174
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said stage. One approach I have used on other systems is to prevent the action buttons appearing. For example, if one did not have the Role of Administrator, one would be prevented from deleting a ticket not created by oneself. However, it did occur...
2
2157
by: Try Guy | last post by:
I'm not sure if I really understand this whole ownderdraw thing... i want to create my own Windows Forms control (NOT USERCONTROL)...is it ownerdrawing I should start learning or something else? And where do you learn this stuff? I've been looking for tutorials but all are for C # or how to use prebuild stuff... I wanna learn how to really do it. If I learn to use Ownerdraw on Menuitems... can I then also use the same knowledge for...
12
3153
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without restarting the application. What I did was to create an AppDomain that loaded the plugins and everything was great, until I tried to pass something else that strings between the domains...
6
6091
by: Tex | last post by:
I am writting a survey system web application. I am using ASP.Net 2, C# and MS SQL 2005. I am able to store surveys and questions associated to the surveys just fine. The problem I am having is Dynamically creating a survey on a page. I have read some MS articles on dynamic creation of web forms and have been able to create one question on a page. I am not sure how to create a variable amount of different types(radio button lists,...
5
2018
by: SalamElias | last post by:
I am creating several chkBoxes dynamically and assigning an event handler in the Page_load as foillows ***************************** Dim chkCatOption As CheckBox = New CheckBox chkCatOption.Text = rowDS.Item("Option_Item_Desc") chkCatOption.ID = rowDS.Item("ID").ToString chkCatOption.Checked = rowDS.Item("HasOption") chkCatOption.AutoPostBack = True AddHandler chkCatOption.CheckedChanged, AddressOf AddOrUpdate
10
4791
by: brooksr | last post by:
I know VB5/VBA very well but have not used VB to create web pages but need to do so. Can someone explain the purposes and differences between VBScript and VB.NET to create web pages? Also where do Visual Web Developer Express Edition and Microsoft Frontpage fit into the picture? It seems there are a bewildering array of choices in creating websites
9
3622
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have to create the dynamic controls in the OnInit stage of the lifecycle. Since data from static controls is not yet available in the OnInit stage I can't know what dynamic control I have to create.
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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 we have to send another system

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.