Design:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace ServerControl2
{
public class Des:System.Web.UI.Design.ControlDesigner
{
public override string GetDesignTimeHtml()
{
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
HyperLink hl = new HyperLink();
hl.Text = "Hello, Control";
hl.NavigateUrl = "http://www.microsoft.com/china/msdn";
hl.RenderControl(tw);
tw.Write(HtmlTextWriter.SpaceChar);
Label lbl = new Label();
lbl.BackColor = System.Drawing.Color.LemonChiffon;
lbl.Text = hl.NavigateUrl;
lbl.RenderControl(tw);
return sw.ToString();
}
}
}
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. LINQ;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Namespace servercontrol2
{
[Designer ("servercontrol2.des, servercontrol2"), defaultproperty ("text")]
[Toolboxdata ("<{0}: servercontrol1 runat = Server> </{0}: servercontrol1>")]
Public class servercontrol1: webcontrol, ipostbackeventhandler
{
[Bindable (true)]
[Category ("appearance")]
[Defaultvalue ("")]
[Localizable (true)]
Public String text
{
Get
{
String S = (string) viewstate ["text"];
Return "custom control AA ";
}
Set
{
Viewstate ["text"] = value;
}
}
Public int speed
{
Get;
Set;
}
Protected override void rendercontents (htmltextwriter output)
{
Output. Write (text );
Output. Write (string. Format (@ "<marquee width = 400 scrollamount = '{0}' behavior = alternate direction = left align = middle>! </Marquee> ", speed ));
}
Protected override void onprerender (eventargs E)
{
}
Protected override void addattributestorender (htmltextwriter writer)
{
Writer. addattribute (htmltextwriterattribute. onclick, "javascript:" + Page. getpostbackeventreference (this ));
Writer. addstyleattribute (htmltextwriterstyle. backgroundcolor, "Red ");
Base. addattributestorender (writer );
}
Public event eventhandler click;
Public void raisepostbackevent (string eventargument)
{
Onclick (eventargs. Empty );
}
Protected virtual void onclick (eventargs ex)
{
If (Click! = NULL)
{
Click (this, ex );
}
// Httpcontext. Current. response. Redirect ("http://www.baidu.com ");
}
}
}