(原创)自已实现服务器控件之 TextBox 控件
声明:本帖只是一个测试Demo,所以,不会写得太规范,也不会考虑到安全性.以方便为 主.所以,用得到的朋友在项目中使用的时候,还希望对其进行改进.
环境
开发工具: VS.net 2003
作者:文刀无尽
日期:2006-02-23
读者要求:有一定的编程经验.
实现功能:
1.在页面回发时,保存文本框中文本.
2.可以选择TextBox的模式
1)单行模式
2)密码模式
3)多行模式
3.自定义事件
原理:
1.在页面回发时,保存文本框中文本.
利用IPostBackDataHandler接口的LoadPostData实现取
到回发的文本.再给属性赋值,这样的话,在页面重新初始化
的时候,就可以保持原来的文本值
2.可以选择TextBox的模式
定义一个模式枚举,在选择不同的模式时,生成
不同的控件.
3.自定义事件
利用IPostBackEventHandler接口的RaisePostBackEvent实现
对回发的事件进行检查,得到相应的回发事件,再激发自定义的
事件.
用IPostBackEventHandler接口后,在客户端会生成如下代码
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
output.AddAttribute("Onkeyup","jscript:"+ Page.GetPostBackEventReference(this, "Onkeyup"));客户端生成如下代码
Onkeyup="jscript:__doPostBack('TextBox2','Onkeyup')"就是利用上面生成的脚本对回发事件进行处理.
各个接口具体作用,参见MSDN
效果图
下面是全部代码
页面代码
Aspx文件
<%@ Register TagPrefix="cc1" Namespace="Skyendless.MyControls" Assembly="MyControls" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ControlsTry.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<cc1:textbox id="TextBox2" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 136px"
runat="server" Height="24px" Width="104px" Text="dgfd"></cc1:textbox><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 424px; POSITION: absolute; TOP: 136px" runat="server"
Height="32px" Width="88px">Label</asp:label>
<cc1:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 184px"
runat="server" Height="32px" Width="128px" Text="sdfdsf" TextMode="Password"></cc1:TextBox>
<cc1:TextBox id="TextBox3" style="Z-INDEX: 104; LEFT: 224px; POSITION: absolute; TOP: 232px"
runat="server" Height="88px" Width="128px" Text="sdfdsfsdfsdfsdfdsf" TextMode="MultiLine"></cc1:TextBox></form>
</body>
</HTML>
Cs文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ControlsTry
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected Skyendless.MyControls.TextBox TextBox2;
protected Skyendless.MyControls.TextBox TextBox1;
protected Skyendless.MyControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
private void Button1_Click(object sender, System.EventArgs e)
{
}
private void TextBox2_keyup(object sender, System.EventArgs e)
{
this.Label1.Text = this.TextBox2.Text;
}

}
}
下面是控件代码
/*****************************************************
*
*功能:实现TextBox服务器控件的部分功能
*作者:文刀无尽
*日期:2006-02-23
***************************************************/
using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace Skyendless.MyControls
{
/// <summary>
/// 类型枚举,单行,密码,多行
/// </summary>
public enum TextBoxMode
{
SingleLine
,Password
,MultiLine
}

[DefaultProperty("Text"),
ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
public class TextBox : System.Web.UI.WebControls.WebControl,IPostBackDataHandler,IPostBackEventHandler
{
[私有变量]
[定义事件]
[自定义属性]
[事件] 

/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
//
//将需要呈现的 HTML 属性和样式添加到指定
//的输出流中
//
AddAttributesToRender(output);
output.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
output.AddAttribute("Onkeyup","jscript:"+ Page.GetPostBackEventReference(this, "Onkeyup"));
SetTextBoxMode(this.TextMode,output);
output.RenderEndTag(); //控件结束
}
private void SetTextBoxMode(TextBoxMode textMode,HtmlTextWriter output)
{
switch (textMode)
{
case TextBoxMode.SingleLine:
output.AddAttribute(HtmlTextWriterAttribute.Value,Text);
output.AddAttribute(HtmlTextWriterAttribute.Type,"text");
output.RenderBeginTag(HtmlTextWriterTag.Input);
break;
case TextBoxMode.MultiLine:
output.AddAttribute(HtmlTextWriterAttribute.Rows,"2");
output.AddAttribute(HtmlTextWriterAttribute.Cols,"10");
output.RenderBeginTag(HtmlTextWriterTag.Textarea);
output.Write(Text);
break;
case TextBoxMode.Password:
output.AddAttribute(HtmlTextWriterAttribute.Value,Text);
output.AddAttribute(HtmlTextWriterAttribute.Type,"password");
output.RenderBeginTag(HtmlTextWriterTag.Input);
break;
default :
output.AddAttribute(HtmlTextWriterAttribute.Type,"text");
output.RenderBeginTag(HtmlTextWriterTag.Input);
break;
}
}
IPostBackDataHandler 成员
IPostBackEventHandler 成员
}
}



浙公网安备 33010602011771号