您的位置:首页 > 编程语言 > ASP

ASP.NET自定义Web服务器控件-TextBox文本框控件

2011-08-26 00:53 393 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

//自定义服务器文本框控件
namespace MyControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox>")]
public class MyTextBox : WebControl,IPostBackDataHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public bool AutoPostBack
{
get
{
if (ViewState["AutoPostBack"]==null) {
ViewState["AutoPostBack"] = false;//默认不是回传
}
return (bool)ViewState["AutoPostBack"];
}

set
{
ViewState["AutoPostBack"] = value;
}
}

protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input; //input标签
}
}
//初始化
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
//加载
protected override void OnLoad(EventArgs e)
{
//Properties(系统资源文件)这是此文本框资源文件里面增加的内容
//[assembly:System.Web.UI.WebResource("MyControls.css.textbox.css","text/css",PerformSubstitution=true)]//添加样式资源
//[assembly: System.Web.UI.WebResource("MyControls.js.textbox.js", "application/x-javascript")]         //添加javaScript资源
//[assembly: System.Web.UI.WebResource("MyControls.images.form_input.png", "image/png")]                //添加图片资源(因为css文件中需要使用此图片设background图片)

//(添加样式步骤2)拿到样式文件的路径
string cssPath = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyControls.css.textbox.css");
//(添加样式步骤3)根据引入外部样式<link rel="stylesheet" href="textbox.css" type="text/css" />这种语法,引入样式
HtmlLink link = new HtmlLink();
link.Href = cssPath;
link.Attributes.Add("rel","stylesheet");
link.Attributes.Add("type", "text/css");
//(添加样式步骤4)在header[头部标签]添加一个样式文件,(添加样式步骤5)在Properties(系统资源文件)中添加样式资源
this.Page.Header.Controls.Add(link);

//(添加JS步骤1)获取js文件路径
string jsPath = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MyControls.js.textbox.js");
//(添加JS步骤2)根据js路径注册js文件
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "textboxJavascript", "<script   src='" + jsPath + "' type='text/javascript'  language= 'javascript'></script> ");
//(添加JS步骤3)在Properties(系统资源文件)中添加JS资源
base.OnLoad(e);
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
this.Attributes.Add("type","text");
this.Attributes.Add("value",Text);
//(添加样式步骤1)增加一个类样式
this.Attributes.Add("class","txt_style");
this.Attributes.Add("name",this.UniqueID);  //回传的时候必须加上此属性才行

if (AutoPostBack)
{
this.Attributes.Add("onchange", "javascript:" + this.Page.GetPostBackEventReference(this)); //自动调用回发事件
}
base.RenderBeginTag(writer);
}
//打印内容
protected override void RenderContents(HtmlTextWriter output)
{
//output.Write(Text);
}

public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string postData = this.Page.Request.Form[this.UniqueID]; //拿到提交文本框的值
if (postData != Text)
{  //如果从页面提交上来的数据不等于原来数据就返回true
Text = postData;  //保存文本框内容
return true;
}
return false;//返回false表示不执行后面的方法
}
public delegate void TxtChangeHandle();
private object key;
public event TxtChangeHandle Change {
add {
this.Events.AddHandler(key, value);
}
remove {
this.Events.RemoveHandler(key, value);
}
}
public void RaisePostDataChangedEvent()
{
//自定义的文本框改变事件
TxtChangeHandle handle = (TxtChangeHandle)base.Events[key];
if (handle != null) {
handle();
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="MyControls" namespace="MyControls" 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:MyTextBox ID="MyTextBox1" AutoPostBack="true" runat="server" />
</div>
</form>
</body>
</html>


.txt_style{ border:none; width:230px; height:25px; background:url(<%=WebResource("MyControls.images.form_input.png")%>)}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: