All HTML elements can be made into HTML server
controls. To do this we place them inside the form control and include the runat server
attribute. The element now becomes a HTML control that we can manipulate using its id
reference. The control in this case is of the HTMLAnchor
class. For this class we can for example set the Href and Title properties, both of which
corresponds to the attributes with the same names. For attributes that don’t have a property,
we can add them to the Attribute Collection as a name/value pair. Another way to create HTML controls is using
only code. To demonstrate, let’s add an image inside
the link, from the Page_Load method in the code page.
We find all the HTML Controls under the System Web UI namespace. But since this namespace
is already included with the using directive, we don’t need to write the full path in order
to create our image. We then set the source and border properties,
and add the image to the anchors Control collection. If we now run the page the image will show
up inside the anchor element. Now if we wanted to add an element that didn’t
have its own HTML control, we would instead use the HtmlGenericControl class. This class
can hold any HTML control and can therefore represent any HTML element.
For example to create a break tag we add the tag to the constructor for that object. Alternatively,
we can just as well use the TagName property, once the object has been created.
We then add the control to the form. This time using the AddAt method to place it before
the Anchor Control. Viewing the page now shows that the break
is indeed added before the link.