代码改变世界

Custom ServerControl

2011-08-26 16:11  Carl Xing  阅读(249)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl.Controls
{
publicclass MostSimpleControl:Control
{
// Control类的呈现架构就是这样在三个方法之间递归的调用,以便把控件的逻辑呈现为html代码。

protectedoverridevoid Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
"alert('you clicked me')");
writer.Write(
"<span>");
writer.Write(
"I am useless,but i am a control!");
writer.Write(
"<span>");

CheckBox rbtn
=new CheckBox();
rbtn.Checked
=true;
this.Controls.Add(rbtn);

RenderChildren(writer);
}

publicoverridevoid RenderControl(HtmlTextWriter writer)
{
this.Render(writer);
}

protectedoverridevoid RenderChildren(HtmlTextWriter writer)
{
foreach (Control child inthis.Controls)
{
child.RenderControl(writer);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ServerControl.Controls
{
publicclass CustomLabel:Label
{
[Description(
"The Label's Default Text")]
publicvirtualstring DefaultText
{
get
{
string s=(string)ViewState["userName"];
return s==null?string.Empty:s;
}
set
{
ViewState[
"userName"] = value;
}
}

protectedoverridevoid RenderContents(System.Web.UI.HtmlTextWriter writer)
{
writer.WriteEncodedText(
"Welcome "+DefaultText);

//writer.WriteEncodedText(this.Text);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;

[assembly: TagPrefix(
"ServerControl", "SControl")]
namespace ServerControl.Controls
{
[DefaultProperty(
"ButtonText")]
publicclass SearchControl : CompositeControl
{
protected TextBox txtBox =new TextBox();
protected Button btn =new Button();

protectedoverridevoid CreateChildControls()
{
//base.CreateChildControls();
this.Controls.Add(txtBox);
this.Controls.Add(btn);

btn.Text
="搜索";
btn.Click
+=new EventHandler(btn_Click);
}


publicevent EventHandler Serch_Click;

protectedvoid btn_Click(object sender, EventArgs e)
{
try
{
Serch_Click(sender, e);
}
catch (Exception ex)
{
//throw new Exception("没有实现Serch_Click委托", ex.InnerException);
}
}

///<summary>
/// 获取或设置录入框文本
///</summary>
publicstring Text
{
get
{
EnsureChildControls();
return txtBox.Text;
}
set
{
EnsureChildControls();
txtBox.Text
= value;
}
}

///<summary>
/// 获取或设置按钮文本
///</summary>
publicstring ButtonText
{
get
{
EnsureChildControls();
return btn.Text;
}
set
{
EnsureChildControls();
btn.Text
= value;
}
}
}
}
    public class ArgButton:Button
    {
        public delegate void OnArgClick(btnEventArgs e);

        public event OnArgClick ArgClick;

        private string Args
        {
            get;
            set;
        }

        public ArgButton()
        { }

        public ArgButton(string args)
        {
            this.Args = args;
            this.Click += new EventHandler(ArgButton_Click);
        }

        void ArgButton_Click(object sender, EventArgs e)
        {
            if (ArgClick != null)
            {
                ArgClick(new btnEventArgs(Args));
            }
        }
    }

    public class btnEventArgs:EventArgs
    {
        private string args;
        public btnEventArgs(string args)
        {
            this.args = args;
        }

        public string Args
        {
            get { return this.args; }
        }
    }

 

default.aspx前后台:(注意:CustomLabel的Text属性无效,因为在重写的时候去掉了base方法。)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ServerControl.Default"%>

<%@ Register assembly="ServerControl" namespace="ServerControl.Controls" 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:MostSimpleControl ID="MostSimpleControl1" runat="server">
</cc1:MostSimpleControl>
<cc1:SearchControl ID="SControl1" OnSerch_Click="SControl1_Serch_Click" runat="server" ButtonText="确定"/>
<cc1:CustomLabel ID="CustomLabel1" Text="ddd" runat="server"></cc1:CustomLabel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ServerControl
{
publicpartialclass Default : System.Web.UI.Page
{
privatefloat getInternetExplorerVersion()
{
float rev =-1;
System.Web.HttpBrowserCapabilities browser
= Request.Browser;
if (browser.Browser =="IE")
rev
= (float)(browser.MajorVersion + browser.MinorVersion);
return rev;
}


protectedvoid Page_Load(object sender, EventArgs e)
{
//SControl1.Serch_Click+=new EventHandler(SControl1_Serch_Click);
CustomLabel1.DefaultText ="jemmy";
}

protectedvoid SControl1_Serch_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(
typeof(Page), "alert", "<script type='text/javascript'>alert('"+SControl1.Text+"')</script>");
}
}
}