473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 CreateChildControls() 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="(Collection)" 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
RegisterRequiresPostback(). 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 1828
Hi Ken,

1. You should always create your controls in the CreateChildControls 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.EnsureChildControls(), it will make sure that CreateChildControls 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 EnsureChildControls(), 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("Appearance"),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden)]
public virtual HolidayCollection 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 CreateChildControls. 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 CreateChildControls() 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="(Collection)" 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
RegisterRequiresPostback(). 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 EnsureChildControls() 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.integrityts.com> wrote in message
news:O3****************@tk2msftngp13.phx.gbl...
Hi Ken,

1. You should always create your controls in the CreateChildControls 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.EnsureChildControls(), it
will make sure that CreateChildControls 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
EnsureChildControls(), 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("Appearance"),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden)] public virtual HolidayCollection 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 CreateChildControls. 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 CreateChildControls() 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="(Collection)" 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
RegisterRequiresPostback(). 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 EnsureChildControls() in your LoadPostData, this will ensure that they've been created and added in the same manner prior to a postback. Note, if the CreateChildControls has previously been called (either by you, or the framework), successively calling EnsureChildControls will not call CreateChildControls.

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 EnsureChildControls() 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.integrityts.com> wrote in message
news:O3****************@tk2msftngp13.phx.gbl...
Hi Ken,

1. You should always create your controls in the CreateChildControls 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.EnsureChildControls(), it
will make sure that CreateChildControls 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
EnsureChildControls(), 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("Appearance"),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden)] public virtual HolidayCollection 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 CreateChildControls. 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 CreateChildControls() 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="(Collection)" 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
RegisterRequiresPostback(). 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
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...
3
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...
22
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...
2
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?...
12
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...
6
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...
5
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...
10
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...
9
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.