自定义服务器控件开发之4:重写的支持多验证TextBox控件 续1
去年写了这个东东,发现还是挺好用的,哈哈!不过感觉功能不太满意,于是有增加了些新的功能。
1、增加了输入提示、自定义错误提示、自定义不能为空的提示;
2、增加输入框处于焦点时的样式变化;
3、增加了可自增加行数和减少行数功能;
4、增加了下拉框模式,下拉框可进行数据源的绑定;
去年写了这个东东,发现还是挺好用的,哈哈!不过感觉功能不太满意,于是有增加了些新的功能。
1、增加了输入提示、自定义错误提示、自定义不能为空的提示;
2、增加输入框处于焦点时的样式变化;
3、增加了可自增加行数和减少行数功能;
4、增加了下拉框模式,下拉框可进行数据源的绑定;


由于字符数过多,不能完全粘贴,需要的朋友请下载附件。
https://files.cnblogs.com/nowind/WebTextBox.rar
增加部分源代码如下:
1、增加了输入提示、自定义错误提示、自定义不能为空的提示;
2、增加输入框处于焦点时的样式变化;
3、增加了可自增加行数和减少行数功能;
4、增加了下拉框模式,下拉框可进行数据源的绑定;


由于字符数过多,不能完全粘贴,需要的朋友请下载附件。
https://files.cnblogs.com/nowind/WebTextBox.rar
增加部分源代码如下:
1
//可调整高度
2
if (this.IsRegulateHeight)
3
{
4
#region
5
StringBuilder sbscript = new StringBuilder();
6
sbscript.Append("\n<!--调节输入框高度 begin-->\n");
7
sbscript.Append(" <script type=\"text/javascript\">\n");
8
sbscript.Append(" //Plus\n");
9
sbscript.Append(" function PlusHeight(id)\n");
10
sbscript.Append(" {\n");
11
sbscript.Append(" var txtInput = document.getElementById(id);\n");
12
sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) + " + this.RegulateRows + ";\n");
13
//sbscript.Append(" txtInput.style.height = txtInput.style.height+15;\n");
14
sbscript.Append(" }\n");
15
sbscript.Append(" //Minus\n");
16
sbscript.Append(" function MinusHeight(id)\n");
17
sbscript.Append(" {\n");
18
sbscript.Append(" var txtInput = document.getElementById(id);\n");
19
sbscript.Append(" if( parseInt(txtInput.rows) >= " + (this.RegulateRows + 1) + " )\n");
20
sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) - " + this.RegulateRows + ";\n");
21
sbscript.Append(" }\n");
22
sbscript.Append(" </script>\n");
23
sbscript.Append("<!--调节输入框高度 end-->\n");
24
25
System.Web.UI.ClientScriptManager client = this.Page.ClientScript;
26
Type cstype = Page.GetType();
27
if (!client.IsStartupScriptRegistered(cstype, "regulateheight"))
28
client.RegisterStartupScript(cstype, "regulateheight", sbscript.ToString());
29
30
StringBuilder sbhtml = new StringBuilder();
31
sbhtml.Append("<div style=\"height:24px;\">");
32
sbhtml.Append(" " + this.ImageIconMinus + " " + this.ImageIconPlus);
33
sbhtml.Append("</div>");
34
output.Write(sbhtml.ToString());
35
#endregion
36
}
37
//下拉框模式
38
if (this.HasDropDownList)
39
{
40
#region
41
//已经改为点击输入框的时候激发客户端事件
42
//output.Write("<img id=\"" + this.ClientID + "Icon\" src=\"" + this.ImageIconShowDropDown + "\" border=\"0\" style=\"cursor:hand;\" onclick=\"ShowList('" + this.ClientID + "');\" align=\"absmiddle\" />\n");
43
int width = Convert.ToInt32(base.Width.Value);
44
width = width == 0 ? 100 : width;
45
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "display:none;position:absolute;");
46
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, this.ClientID + "List");
47
output.RenderBeginTag(HtmlTextWriterTag.Div);
48
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "margin-left:" + width + "px;");
49
output.RenderBeginTag(HtmlTextWriterTag.Span);
50
51
drpList.ID = base.ID+"Items";
52
drpList.Style.Add("margin-left", "-" + width + "px");
53
drpList.Attributes.Add("onchange", "document.getElementById('" + this.ClientID + "').value=this.options[this.selectedIndex].text;document.getElementById('" + this.ClientID + "List').style.display='none';");
54
//drpList.Attributes.Add("onblur", "this.style.display='none'");
55
if (this.DropDownListDataSource !=null && this.DropDownListDataSource.Rows.Count > 0)
56
{
57
drpList.DataTextField = this.DataTextField;
58
drpList.DataValueField = this.DataValueField;
59
drpList.DataSource = this.DropDownListDataSource.DefaultView;
60
drpList.DataBind();
61
}
62
if(HasDefaultItem)
63
drpList.Items.Insert(0,new ListItem(this.DefaultItemText,this.DefaultItemValue));
64
foreach (ListItem item in drpList.Items)
65
{
66
if (item.Text == base.Text.Trim())
67
{
68
item.Selected = true;
69
break;
70
}
71
}
72
this.drpList.RenderControl(output);
73
output.RenderEndTag();
74
output.RenderEndTag();
75
76
StringBuilder sbscript = new StringBuilder();
77
js内容#region js内容
78
sbscript.Append("\n<script type=\"text/javascript\">\n");
79
sbscript.Append(" <!--下拉框显示隐藏 begin-->\n");
80
sbscript.Append(" function ShowList(id)\n");
81
sbscript.Append(" {\n");
82
sbscript.Append(" var txtInput = document.getElementById(id);\n");
83
sbscript.Append(" var drpList = document.getElementById(id+'List');\n");
84
//sbscript.Append(" var drpIcon = document.getElementById(id+'Icon');\n");
85
sbscript.Append(" var drpItems = document.getElementById(id+'Items');\n"); //下拉框
86
sbscript.Append(" if(drpList.style.display == 'block')\n");
87
sbscript.Append(" {\n");
88
sbscript.Append(" txtInput.title = '点击显示下拉框';\n");
89
sbscript.Append(" drpList.style.display = 'none';\n");
90
//sbscript.Append(" drpIcon.src = '" + this.ImageIconShowDropDown + "';\n");
91
sbscript.Append(" }\n");
92
sbscript.Append(" else\n");
93
sbscript.Append(" {\n");
94
sbscript.Append(" txtInput.title = '点击关闭下拉框';\n");
95
sbscript.Append(" drpList.style.display = 'block';\n");
96
sbscript.Append(" drpList.style.left = GetOffsetLeft(txtInput)+'px';\n");
97
sbscript.Append(" drpList.style.top = (GetOffsetTop(txtInput)+1)+'px';\n");
98
sbscript.Append(" drpItems.style.width = txtInput.offsetWidth+'px';\n");
99
//sbscript.Append(" drpIcon.src = '" + this.ImageIconHideDropDown + "';\n");
100
sbscript.Append(" }\n");
101
sbscript.Append(" }\n");
102
sbscript.Append(" <!--下拉框显示隐藏 end-->\n");
103
sbscript.Append(" function GetOffsetTop(e) \n");
104
sbscript.Append(" {\n");
105
sbscript.Append(" var offsetTop = e.offsetTop;\n");
106
sbscript.Append(" var offsetParent = e.offsetParent;\n");
107
sbscript.Append(" while(offsetParent)\n");
108
sbscript.Append(" {\n");
109
sbscript.Append(" offsetTop += offsetParent.offsetTop;\n");
110
sbscript.Append(" offsetParent = offsetParent.offsetParent;\n");
111
sbscript.Append(" }\n");
112
sbscript.Append(" return offsetTop+e.offsetHeight;\n");
113
sbscript.Append(" }\n");
114
sbscript.Append(" function GetOffsetLeft(e) \n");
115
sbscript.Append(" {\n");
116
sbscript.Append(" var offsetLeft = e.offsetLeft;\n");
117
sbscript.Append(" var offsetParent = e.offsetParent;\n");
118
sbscript.Append(" while(offsetParent) \n");
119
sbscript.Append(" {\n");
120
sbscript.Append(" offsetLeft += offsetParent.offsetLeft;\n");
121
sbscript.Append(" offsetParent = offsetParent.offsetParent;\n");
122
sbscript.Append(" }\n");
123
sbscript.Append(" return offsetLeft;\n");
124
sbscript.Append(" }\n");
125
sbscript.Append("</script>\n");
126
# endregion
127
System.Web.UI.ClientScriptManager client = this.Page.ClientScript;
128
Type cstype = Page.GetType();
129
if (!client.IsStartupScriptRegistered(cstype, "showlist"))
130
client.RegisterStartupScript(cstype, "showlist", sbscript.ToString());
131
132
#endregion
133
}
134
//可调整高度2
if (this.IsRegulateHeight)3

{4

#region5
StringBuilder sbscript = new StringBuilder();6
sbscript.Append("\n<!--调节输入框高度 begin-->\n");7
sbscript.Append(" <script type=\"text/javascript\">\n");8
sbscript.Append(" //Plus\n");9
sbscript.Append(" function PlusHeight(id)\n");10
sbscript.Append(" {\n");11
sbscript.Append(" var txtInput = document.getElementById(id);\n");12
sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) + " + this.RegulateRows + ";\n");13
//sbscript.Append(" txtInput.style.height = txtInput.style.height+15;\n");14
sbscript.Append(" }\n");15
sbscript.Append(" //Minus\n");16
sbscript.Append(" function MinusHeight(id)\n");17
sbscript.Append(" {\n");18
sbscript.Append(" var txtInput = document.getElementById(id);\n");19
sbscript.Append(" if( parseInt(txtInput.rows) >= " + (this.RegulateRows + 1) + " )\n");20
sbscript.Append(" txtInput.rows = parseInt(txtInput.rows) - " + this.RegulateRows + ";\n");21
sbscript.Append(" }\n");22
sbscript.Append(" </script>\n");23
sbscript.Append("<!--调节输入框高度 end-->\n");24

25
System.Web.UI.ClientScriptManager client = this.Page.ClientScript;26
Type cstype = Page.GetType();27
if (!client.IsStartupScriptRegistered(cstype, "regulateheight"))28
client.RegisterStartupScript(cstype, "regulateheight", sbscript.ToString());29

30
StringBuilder sbhtml = new StringBuilder();31
sbhtml.Append("<div style=\"height:24px;\">");32
sbhtml.Append(" " + this.ImageIconMinus + " " + this.ImageIconPlus);33
sbhtml.Append("</div>");34
output.Write(sbhtml.ToString());35
#endregion36
}37
//下拉框模式38
if (this.HasDropDownList)39

{40

#region41
//已经改为点击输入框的时候激发客户端事件42
//output.Write("<img id=\"" + this.ClientID + "Icon\" src=\"" + this.ImageIconShowDropDown + "\" border=\"0\" style=\"cursor:hand;\" onclick=\"ShowList('" + this.ClientID + "');\" align=\"absmiddle\" />\n");43
int width = Convert.ToInt32(base.Width.Value);44
width = width == 0 ? 100 : width;45
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "display:none;position:absolute;");46
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, this.ClientID + "List");47
output.RenderBeginTag(HtmlTextWriterTag.Div);48
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "margin-left:" + width + "px;");49
output.RenderBeginTag(HtmlTextWriterTag.Span);50

51
drpList.ID = base.ID+"Items";52
drpList.Style.Add("margin-left", "-" + width + "px");53
drpList.Attributes.Add("onchange", "document.getElementById('" + this.ClientID + "').value=this.options[this.selectedIndex].text;document.getElementById('" + this.ClientID + "List').style.display='none';");54
//drpList.Attributes.Add("onblur", "this.style.display='none'");55
if (this.DropDownListDataSource !=null && this.DropDownListDataSource.Rows.Count > 0) 56

{57
drpList.DataTextField = this.DataTextField;58
drpList.DataValueField = this.DataValueField;59
drpList.DataSource = this.DropDownListDataSource.DefaultView;60
drpList.DataBind();61
}62
if(HasDefaultItem)63
drpList.Items.Insert(0,new ListItem(this.DefaultItemText,this.DefaultItemValue));64
foreach (ListItem item in drpList.Items)65

{66
if (item.Text == base.Text.Trim())67

{68
item.Selected = true;69
break;70
}71
}72
this.drpList.RenderControl(output);73
output.RenderEndTag();74
output.RenderEndTag();75

76
StringBuilder sbscript = new StringBuilder();77

js内容#region js内容78
sbscript.Append("\n<script type=\"text/javascript\">\n");79
sbscript.Append(" <!--下拉框显示隐藏 begin-->\n");80
sbscript.Append(" function ShowList(id)\n");81
sbscript.Append(" {\n");82
sbscript.Append(" var txtInput = document.getElementById(id);\n");83
sbscript.Append(" var drpList = document.getElementById(id+'List');\n");84
//sbscript.Append(" var drpIcon = document.getElementById(id+'Icon');\n");85
sbscript.Append(" var drpItems = document.getElementById(id+'Items');\n"); //下拉框86
sbscript.Append(" if(drpList.style.display == 'block')\n");87
sbscript.Append(" {\n");88
sbscript.Append(" txtInput.title = '点击显示下拉框';\n");89
sbscript.Append(" drpList.style.display = 'none';\n");90
//sbscript.Append(" drpIcon.src = '" + this.ImageIconShowDropDown + "';\n");91
sbscript.Append(" }\n");92
sbscript.Append(" else\n");93
sbscript.Append(" {\n");94
sbscript.Append(" txtInput.title = '点击关闭下拉框';\n");95
sbscript.Append(" drpList.style.display = 'block';\n");96
sbscript.Append(" drpList.style.left = GetOffsetLeft(txtInput)+'px';\n");97
sbscript.Append(" drpList.style.top = (GetOffsetTop(txtInput)+1)+'px';\n");98
sbscript.Append(" drpItems.style.width = txtInput.offsetWidth+'px';\n");99
//sbscript.Append(" drpIcon.src = '" + this.ImageIconHideDropDown + "';\n");100
sbscript.Append(" }\n");101
sbscript.Append(" }\n");102
sbscript.Append(" <!--下拉框显示隐藏 end-->\n");103
sbscript.Append(" function GetOffsetTop(e) \n");104
sbscript.Append(" {\n");105
sbscript.Append(" var offsetTop = e.offsetTop;\n");106
sbscript.Append(" var offsetParent = e.offsetParent;\n");107
sbscript.Append(" while(offsetParent)\n");108
sbscript.Append(" {\n");109
sbscript.Append(" offsetTop += offsetParent.offsetTop;\n");110
sbscript.Append(" offsetParent = offsetParent.offsetParent;\n");111
sbscript.Append(" }\n");112
sbscript.Append(" return offsetTop+e.offsetHeight;\n");113
sbscript.Append(" }\n");114
sbscript.Append(" function GetOffsetLeft(e) \n");115
sbscript.Append(" {\n");116
sbscript.Append(" var offsetLeft = e.offsetLeft;\n");117
sbscript.Append(" var offsetParent = e.offsetParent;\n");118
sbscript.Append(" while(offsetParent) \n");119
sbscript.Append(" {\n");120
sbscript.Append(" offsetLeft += offsetParent.offsetLeft;\n");121
sbscript.Append(" offsetParent = offsetParent.offsetParent;\n");122
sbscript.Append(" }\n");123
sbscript.Append(" return offsetLeft;\n");124
sbscript.Append(" }\n");125
sbscript.Append("</script>\n");126
# endregion127
System.Web.UI.ClientScriptManager client = this.Page.ClientScript;128
Type cstype = Page.GetType();129
if (!client.IsStartupScriptRegistered(cstype, "showlist"))130
client.RegisterStartupScript(cstype, "showlist", sbscript.ToString());131

132
#endregion133
}134

浙公网安备 33010602011771号