.net 复合控件的开发

using System;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.ComponentModel;
  using System.ComponentModel.Design;
  namespace WebControlLibrary{

public class 用户登录: CompositeControl {

  //声明变量

private Button _button;//登录按钮
private TextBox _textBoxname;//账号输入筐
private TextBox _textBoxpassword;//密码输入筐
private Label _labcode;
private CheckBox _checkbox;
private string names, passwords, code;//接受用户的密码和账号等信息
private bool _jizhu=false;
private static readonly object EventSubmitKey = new object();
private logincjing.用户操作接口 _用户操作=null;


  用户登录(logincjing.用户操作接口 操作接口){
        _用户操作 = 操作接口;
    }


  //定义属性ButtonText,用于指定按钮上的文字

  [

  Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字")

  ]

  public string ButtonText {

  get {

  EnsureChildControls();
return _button.Text;

  }

  set {

  EnsureChildControls();

  _button.Text = value;

  }

  }

  //定义属性labcode,用于指定按钮上的文字

  [

  Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字")

  ]

  public string labText
  {

      get
      {

          EnsureChildControls();
          return _labcode.Text;

      }

      set
      {

          EnsureChildControls();

          _labcode.Text =value;

      }

  }

// 定义密码筐
[
Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本")
]
public TextBoxMode TextMode {

    get
    {
        EnsureChildControls();
        return _textBoxpassword.TextMode;
    }

    set{
        EnsureChildControls();
        _textBoxpassword.TextMode = value;
    }

}

  // 实现事件属性结构

  public event EventHandler Submit {

  add {

  Events.AddHandler(EventSubmitKey, value);

  }

  remove {

  Events.RemoveHandler(EventSubmitKey, value);

  }

  }

  // 实现OnSubmit

  protected virtual void OnSubmit(EventArgs e) {

    //这里是处理单击事件的过程
  
    this.names = _textBoxname.Text.ToString().Trim();
    this.passwords= _textBoxpassword.Text.ToString().Trim();
    this.code = _labcode.Text.ToString().Trim();
      if(_checkbox.Checked){
          _jizhu = true;
      }
    //this._button.Text = this.names + this.passwords;

      //用户登录 测试 = new 用户登录();

    _用户操作.登录(names,passwords,_jizhu);


  EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
 
  if (SubmitHandler!= null) {
    
  SubmitHandler(this, e);

  }

  }

  // 实现Submit事件引发的事件处理程序

  private void _button_Click(Object source, EventArgs e) {
    
  OnSubmit(EventArgs.Empty);

  }

  // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法

  protected override void RecreateChildControls() {

  EnsureChildControls();

  }

  //重写CreateChildControls方法,将子控件添加到复合控件中

  protected override void CreateChildControls() {

  Controls.Clear();

  _button = new Button();
  _textBoxname = new TextBox();
  _textBoxpassword = new TextBox();
_labcode = new Label();
_checkbox = new CheckBox();
_labcode.Text = new Random(2).Next(3).ToString();
    _button.ID = "btn";

  _button.Click += new EventHandler(_button_Click);

  this.Controls.Add(_button);

  this.Controls.Add(_textBoxname);
this.Controls.Add(_textBoxpassword);

  }

  //重写Render方法,呈现控件中其他的HTML代码

  protected override void Render(HtmlTextWriter output) {

  output.AddAttribute(HtmlTextWriterAttribute.Border, "0px");

  output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5px");

  output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px");

  output.RenderBeginTag(HtmlTextWriterTag.Table);

  output.RenderBeginTag(HtmlTextWriterTag.Tr);

  output.RenderBeginTag(HtmlTextWriterTag.Td);

  _textBoxname.RenderControl(output);
  _textBoxpassword.RenderControl(output);
  _labcode.RenderControl(output);
  _checkbox.RenderControl(output);
  output.RenderEndTag();

  output.RenderBeginTag(HtmlTextWriterTag.Td);

  _button.RenderControl(output);

  output.RenderEndTag();

  output.RenderEndTag();

  output.RenderEndTag();

  }

  }

  }



你可能感兴趣的:(UI,Web,using,design,webcontrols)