Using system;
Using system. Web. UI;
Namespace simplecontrol
{
Public class simplecontrol: Control
{
Protected override void render (htmltextwriter writer)
{
Writer. Write ("I don't do anything useful ,");
Writer. Write ("but at least I'm a control ...");
}
}
}
System. Web. UI. htmltextwriter is a tool class,
It encapsulates the function of writing HTML into text streams.
Simplecontrol simply transfers text to the write method of the htmltextwriter instance.
It is equivalent to the functions implemented by response. Write, which transfers the specified text to the HTTP Response stream.
Do not directly call page. response. Write from the control, because this will break the page framework encapsulation.
Create a Project B.
Deploy the user control just created
___
Compile and deploy server controls:
Change the previous project generation path to the bin directory of Project B and regenerate
___
Use custom server controls:
Go to Project B
On the default. aspx page of the reference control, add:
<% @ Register tagprefix = "MSP" namespace = "mspress. servercontrols" assembly = "mspress. servercontrols" %>
Tagprefix: creates an alias.
Namespace: Specifies the namespace declared by the custom control.
Assembly: Specifies the accessory that the control will generate.
Add
<Body>
<Form ID = "form1" runat = "server">
<Div>
<MSP: simplecontrol id = "simple1" runat = "server"/>
</Div>
</Form>
</Body>