473,386 Members | 1,699 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,386 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 1834
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.