SimpleControl extendsing System.Web.UI.WebControls.WebControl
<%@ Page language="C#" %>
<%@ Register TagPrefix="Control" Namespace="Control" Assembly="Control" %>
<script language="C#" runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Random rand = new Random();
SimpleControl1.Text = rand.Next(1,100).ToString();
}
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<body>
<form id="Form1" method="post" runat="server">
<Control:SimpleControl id="SimpleControl1" runat="server" />
<asp:linkbutton text="PostBack test" runat="server" />
<p>
Values are not maintained across postbacks. <a href="Control.aspx">Re-load</a>
</p>
</form>
</body>
</html>
File: Control.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace Control
{
[DefaultProperty("Text"), ToolboxData("<{0}:Control runat=server></{0}:Control>")]
public class SimpleControl : System.Web.UI.WebControls.WebControl
{
private string _text;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
protected override void Render(HtmlTextWriter output)
{
output.Write(Text);
}
}
}
Related examples in the same category