Introduction
Will need to create user
control really just pages embedded within other pages that you can use that
technology to create controls like the other controls that are built into
the.net framework if you want to create controls the look and feel like the
built in one's got right some code does the way around it will take a little
more effort but you'll see that the benefits outweigh the effort because you'll
have complete control over what you're controlling this in the output page in
this section will look at starting with the basic template that Microsoft
provides for creating a custom control and then we'll revisit the state list
user control and we write it as a custom control to see how much effort is
involved will finish by adding some ease-of-use features such as providing a
default property a default events for the custom control.
Custom
Controls 1
In order to create the new custom control
all create a new project this project will be a web project@ASP.net server
control all except the default name and location that's fine with me and yes I
will not save my changes to that project in the new custom control project
there's a class name server control 1.VB first of all note that this thing
inherits from the web control class the web control class provide some built-in
functionality for creating custom controls here we have a number of attributes
that define the behavior of this property but we also have make it clear it
user rated we have a text property this control.
This
sample template if we disregard these attributes for now of these attributes
will come back and look at those later the text property is pretty simple it
has a string tests it gets its value from of view state variable name text this
verbal stored in view state so it will survive a round-trip to the server along
with this page if S is nothing then we return the name of this control less of
the idea of the controls in square brackets otherwise return the text which
came from the view state property that's the property getter the property
setter this takes the value we want to supply the text property and puts it
into the view state variable name text.
So
that's the code that handles getting and setting the text property of this
control that's really the only property has there's also down here and override
for the render contents method of the web control base class the render
contents method gets called with ASP.net needs to draw the contents of the
control onto the output page and what is this control do when asked to render
its contents does nothing more than take its text property and white it into
the output stream the HTML text writer represents the stream into which were
writing the HTML output the additional text writer has a couple of tricks up
its sleeve that make it easy.
