软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP教程 > Web控件开发系列(四) 自定义控件属性

Web控件开发系列(四) 自定义控件属性

控件在WEB开发时经常要用到,虽然有部分已经存在工具箱里,但有时总需要根据自己的要求,开发一些合适自己的控件。

1、 系统属性: 当开发控件你选择了继承基类,例如你继承了WebContorl或Control,你自定义的控件就会有WebControl或Control类的系统属性。(这节还是用上节的例子代码,只是添加了属性设置的代码)

2、 自定义的属性: 例如下面就是自定义属性设置的代码:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ServerControl
{
[DefaultProperty ("TextBoxValue")]
[ToolboxData ("<{0}:RenderOutPut runat=server></{0}:RenderOutPut>")]
public class RenderOutPut : CompositeControl
{
private Label _lblUser;
private Label _lblPassWrod;
private TextBox _txtUser;
private TextBox _txtPassWord;
private Button _bntOK;
private Button _bntReset;

public RenderOutPut()
{ }

[Category("LabelTextBox")]
[Description("用户名标签信息显示")]
public string lblUserText
{
get
{
this.EnsureChildControls();
return this._lblUser.Text;
}
set
{
this.EnsureChildControls();
this._lblUser.Text = value;
}

}

[Category("LabelTextBox")]
[Description("密码标签信息显示")]
public string lblPassWrodText
{
get
{
this.EnsureChildControls();
return this._lblPassWrod.Text;
}
set
{
this.EnsureChildControls();
this._lblPassWrod.Text = value;
}

}

[Category("LabelTextBox")]
[Description("用户名信息显示")]
public string txtUserText
{
get
{
this.EnsureChildControls();
return this._txtUser.Text;
}
set
{
this.EnsureChildControls();
this._txtUser.Text = value;
}

}

[Category("LabelTextBox")]
[Description("密码信息显示")]
public string txtPassWordText
{
get
{
this.EnsureChildControls();
return this._txtPassWord.Text;
}
set
{
this.EnsureChildControls();
this._txtPassWord.Text = value;
}

}

[Category("LabelTextBox")]
[Description("确认按钮信息显示")]
public string bntOKText
{
get
{
this.EnsureChildControls();
return this._bntOK.Text;
}
set
{
this.EnsureChildControls();
this._bntOK.Text = value;
}

}

[Category("LabelTextBox")]
[Description("重设按钮信息显示")]
public string bntResetText
{
get
{
this.EnsureChildControls();
return this._bntReset.Text;
}
set
{
this.EnsureChildControls();
this._bntReset.Text = value;
}

}

/// <summary>
/// 生成控件
/// </summary>
protected override void CreateChildControls()
{
this.Controls.Clear();

_lblUser = new Label();
_lblUser.Text = "用户名: ";
_lblUser.Height = Unit.Pixel(18); //高度与长度的数值你们按自己的意愿去调
_lblUser.ID = "lblUser";
this.Controls.Add(_lblUser);

_lblPassWrod = new Label();
_lblPassWrod.Text = "密 码: ";
_lblPassWrod.Height = Unit.Pixel(18);
_lblPassWrod.ID = "lblPassWrod";
this.Controls.Add(_lblPassWrod);

_txtUser = new TextBox();
_txtUser.Width = Unit.Pixel(100);
_txtUser.Height = Unit.Pixel(18);
_txtUser.ID = "txtUser1";
this.Controls.Add(_txtUser);

_txtPassWord = new TextBox();
_txtPassWord.Width = Unit.Pixel(100);
_txtPassWord.TextMode = TextBoxMode.Password;
_txtPassWord.Height = Unit.Pixel(18);
_txtPassWord.ID = "txtPassWord";
this.Controls.Add(_txtPassWord);

_bntOK = new Button();
_bntOK.Text = " OK ";
_bntOK.ID = "bntOK";
this.Controls.Add(_bntOK);

_bntReset = new Button();
_bntReset.Text = "Reset";
_bntReset.ID = "bntReset";
this.Controls.Add(_bntReset);

this.ChildControlsCreated = true;
}

protected override void RecreateChildControls()
{
if (this.ChildControlsCreated == false)
{
base.RecreateChildControls();
}
}

/// <summary>
/// 输出控件
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, " 0");
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
writer.RenderBeginTag(HtmlTextWriterTag.Table);

writer.RenderBeginTag(HtmlTextWriterTag.Tr); //第一行开始
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write("用户注册");
writer.RenderEndTag(); //第一个单元格结束
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write("");
writer.RenderEndTag(); //第二个单元格结束
writer.RenderEndTag(); //第一行结束

writer.RenderBeginTag(HtmlTextWriterTag.Tr); //第一行开始
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._lblUser.RenderControl(writer);
writer.RenderEndTag(); //第一个单元格结束
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._txtUser.RenderControl(writer);
writer.RenderEndTag(); //第二个单元格结束
writer.RenderEndTag(); //第一行结束

writer.RenderBeginTag(HtmlTextWriterTag.Tr); //第二行开始
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._lblPassWrod.RenderControl(writer);
writer.RenderEndTag(); //第一个单元格结束
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._txtPassWord.RenderControl(writer);
writer.RenderEndTag(); //第二个单元格结束
writer.RenderEndTag(); //第二行结束

writer.RenderBeginTag(HtmlTextWriterTag.Tr); //第三行开始
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._bntOK.RenderControl(writer);
writer.RenderEndTag(); //第一个单元格结束
writer.RenderBeginTag(HtmlTextWriterTag.Td);
this._bntReset.RenderControl(writer);
writer.RenderEndTag(); //第二个单元格结束
writer.RenderEndTag(); //第三行结束

writer.RenderEndTag(); //结束table
}
}
}
注意: 上面代码中Category与Description就是属性的设计时特性,下面列出部分.NET提供的特性:

相关阅读
栏目导航
推荐软件