You'll
see later that you have to do something to make that happen and here all the
set text box 1.txt equal to state drop-down list one.value thought you take so
will take the value of the drop-down list and place it into the text box Save
it all was run this page will selected value and it works correctly doesn't
work faster than the user control that though once it's on the page ASP.net
doesn't care with the code came from it still works the same way the real
differences this control has a slightly better design time experience.
Because
it appears on the toolbox weakens drag it onto any page and what sincere we
treat it like any other server control how does this control to his job let's
look at the code before go to Florida here I want to look at properties and
methods of this HTML text writer class we might be using is not look at the
code yet was look at properties methods of writer the one that's useful is
added attribute at attribute adds the specified markup and value to the opening
Tagamet element that's useful we had already seen the right method there's the
right attribute method.
This
one just writes the markup attribute value to the output stream were ever we are
within the output is also you see the right begin tag right begin tag begins to
write the output of the beginning of a tag) tag writes the complete and tag out
there's right lie which rights out line obviously there's a default tab string
the default tab string present a single tab character in case what is a tab
into the output of your HTML and there also is the tag right care would outputs
the closing angle bracket so should you want to use this writer to piece
together the beginning portion of an element that some attributes in the
closing right character you would use of this property to do the work so.
Now let's start back and
look and see how this control doesn't strong the first thing it does is in the
public constructor is called the base constructor and tells it what kind of
element were creating this creates the output select tag around all the
contents of this control is nice of the constructor accepts a parameter so you
have to worry about creating the outermost element if step one was creating the
control and calling the base class's constructor then step two involves adding
control attributes this control requires some attributes it needs the base
class attributes like style and position those things are generally provided by
the base web control class we also need a unique ID which is required for data
post back remember were try to keep track of the selected state we have
survival post back sweetie unique ID to do that if auto post back is true we
need an on change attribute which indicates client-side script to run.
That
outputs Will reinvestigate our state drop-down list treating it as a custom
control this time our user control version of the state drop-down list control
was fine but what if you want a custom control what if you wanted to appear the
toolbox like other controls what you want compiled into a separate assemblies
was easy to deploy like other controls are sample includes a state drop-down
list custom control in order to use this you to add the state drop-down list
control the toolbox attitude to page set.
Its
auto post back property to true add code to do something with the selected
value better rendered this control you have to remember that because it's a
custom control it has to render all of its own HTML you need to end up with
HTML that looks something like this that is we the select element a name
property and ID property ASP.net requires the ID property then we need option elements
for each option in the list and one of them is selected so we need the selected
attribute for the one that is selected along with value and text information.
For
each item I like will we created the user control for the custom control we
have to write code bit by bit output exactly this HTML to make this happen were
going to use the HTML text writer class which you've already seen it in action
the render and render contents methods of the base web control class are passed
out HTML text writer object it's a stream into which you have to write the HTML
output this class provides many methods and properties for creating HTML
content it also contains several enumerations containing constants as well your
party see that because we use the constants for creating a span element I've
loaded the state list solution which includes our state drop-down list web
control.
You
can see here we are class that inherits from the web control class before we
investigate this code I'd like to try it out to do that I'll add to my solution
a new website cultures ASP.net websites in the filesystem the name doesn't
really matter to me saw click okay let's build this thing there we go and show
this in design view and now in the toolbox there's a state drop-down list
Dragon instance of it onto the page there is the state drop-down list that me
set this is the start of project tries to run this where person five we make
sure the auto post back property on this control is set to true there we go is
for the text box on here as well there we go and now I'd like to react to an
event of this control saw go to the events list and choose the selected index
changed event actually I can make it easier I just double-click on the control
and this goes automatically to the selected index changed event of this control
and that requires some effort.
And
quick to render HTML content at this point were just slamming out the text of
this control will lets you use this control on the page go to add it to my
solution a website that's fine that's fine that's fine limit rebuild everything
by rebuilding everything I allow Visual Studio to play a fun trip and that is
here in design view in the toolbox window once I've built my solution Visual
Studio season has this server control available for use on this page it looks
like a server control.
So they supply it as an
option on the toolbox window I could drag this onto the page and because right
now my server control has no text property it displays its ID property in
square brackets the class they the server control one as the first instance of
that control so as a server control one number one and if I come your change
the text to the obligatory hello world the control updates to display that text
will lets you run this page and see what's really going output when ASP.net
renders the page with ASP.net renders this page we see in the browser for look
at the source here a span tag.
Which
contains our control and here's hello world actually the span tag is how
ASP.net represents a label is treating this like a label control well that's
good enough we see that they've managed to spit out the HTML is required for
this control what if I want to say a few have some I can come to my control and
here in the override for the render contents method I just need to spit out
some more HTML so I need to come out here and add a call to output.adding some
way to write out the beginning of the span tag is a right for all begin tag
methods courses also a right begin tag method which rights the opening tag with
room to put attributes so the right begin tag won't put the closing bracket
write full begin tag closes the opening tag of our span element.
So
I need here to figure how I put the span out this needs to be a strain I could
just type the word span about 12 I will use HTML text writer tag is I love this
because they give us enumeration which is all of the possible HTML elements I
want span.2 strings there we go that's the begin tag at the end money the output.
Right and tag HTML text writer tag.span.to string there we go save this go back
to our page this rebuild everything I was view the page the browser and see
what happens it looks the same if we look at the source you see that what we
spit out now is a little more complicated the contents of our external span
contains an internal span with the text property of our custom control in there
so we've created a custom control and modify the HTML.
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
Controls1
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.