public class PicShow : Control
{
private string _imgUrl;
//属性
public virtual string ImgUrl
{
get
{
return this._imgUrl;
}
set
{
this._imgUrl = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100px");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//Create Img Tag
writer.AddAttribute(HtmlTextWriterAttribute.Src, this.ImgUrl);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "80px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "80px");
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
//End Of Div
writer.RenderEndTag();
}
}
<%@ Register Assembly="HenllyeeConrol" Namespace="MyConrol1" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>示例</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:PicShow ID="" runat="server"
ImgUrl="Images/1109508867.jpg">
</cc1:PicShow>
<asp:Button ID="btnChange" Text="Change Picture" runat="server"
onclick="btnChange_Click" />
</div>
</form>
</body>
</html>
protected void btnChange_Click(object sender, EventArgs e)
{
if (this.psDemo.ImgUrl == "Images/1109508867.jpg")

this.psDemo.ImgUrl = "Images/bg.jpg";
else

this.psDemo.ImgUrl = "Images/1109508867.jpg";
}
public class viewStatePic : PicShow
{
public override string ImgUrl
{
get
{
string strImgUrl = (string)ViewState["ImgUrl"];
return (strImgUrl == null) ? String.Empty : strImgUrl;
}
set
{
ViewState["ImgUrl"] = value;
}
}
}
public class ControlStatePic : Albumn
{
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresControlState(this);
base.OnInit(e);
}
protected override object SaveControlState()
{
return this.ImgUrl;
}
//从保存的控件视图中取出来
protected override void LoadControlState(object savedState)
{
this.ImgUrl = savedState as string;
}
}

