对于B/S的系统,在页面控制上不如C/S系统那么控制灵活,就比如现在一个页面或用户控件里有两个按钮button1和button2,两个文本框Textbox1和Textbox2,当您想控制当焦点在Textbox1时回车提交button1,在Textbox2时提交button2,这个时候就需要通过客户端脚本来实现和服务器端控件的交互,这是我写的封装好的类,
控件继承 TextBox,SubmitControl属性用来指定此文本框上按回车时由哪个按钮来响应。
SubmitableControlConvertor增加设计时支持,就是在设计时可以用下拉框选择提交按钮。
目前只考虑了Button、LinkButton和ImageButton作为提交按钮,当然继承它们的控件也可以。
1
using
System;
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.ComponentModel;
5
6 namespace MyCompany.Controls
7 {
8 public class SubmitTextBox : TextBox
9 {
10 /// <summary>
11 /// 用来指定按回车时由哪个按钮来响应
12 /// </summary>
13 [TypeConverter( typeof (SubmitableControlConvertor)), DefaultValue( "" ),Category( " Behavior " )]
14 public string SubmitControl
15 {
16 get
17 {
18 object ret = this .ViewState[ " SubmitControl " ];
19 if (ret != null )
20 {
21 return ( string ) ret;
22 }
23 return string .Empty;
24 }
25 set
26 {
27 this .ViewState[ " SubmitControl " ] = value;
28 }
29 }
30
31 protected override void AddAttributesToRender(HtmlTextWriter writer)
32 {
33 base .AddAttributesToRender(writer);
34 if ( this .SubmitControl.Length > 0 )
35 {
36 Control con = FindControl(SubmitControl);
37 if (con != null )
38 {
39 string script = " if(event.keyCode == 13){document.getElementById(' " + con.ClientID + " ').click();event.returnValue=false;} " ;
40 writer.AddAttribute( " onkeydown " ,script);
41 }
42 }
43 }
44 }
45 }
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.ComponentModel;
5
6 namespace MyCompany.Controls
7 {
8 public class SubmitTextBox : TextBox
9 {
10 /// <summary>
11 /// 用来指定按回车时由哪个按钮来响应
12 /// </summary>
13 [TypeConverter( typeof (SubmitableControlConvertor)), DefaultValue( "" ),Category( " Behavior " )]
14 public string SubmitControl
15 {
16 get
17 {
18 object ret = this .ViewState[ " SubmitControl " ];
19 if (ret != null )
20 {
21 return ( string ) ret;
22 }
23 return string .Empty;
24 }
25 set
26 {
27 this .ViewState[ " SubmitControl " ] = value;
28 }
29 }
30
31 protected override void AddAttributesToRender(HtmlTextWriter writer)
32 {
33 base .AddAttributesToRender(writer);
34 if ( this .SubmitControl.Length > 0 )
35 {
36 Control con = FindControl(SubmitControl);
37 if (con != null )
38 {
39 string script = " if(event.keyCode == 13){document.getElementById(' " + con.ClientID + " ').click();event.returnValue=false;} " ;
40 writer.AddAttribute( " onkeydown " ,script);
41 }
42 }
43 }
44 }
45 }
1
using
System;
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.ComponentModel;
5 using System.Collections;
6
7 namespace MyCompany.Controls
8 {
9 public class SubmitableControlConvertor : StringConverter
10 {
11 private object [] GetControls(IContainer container)
12 {
13 ComponentCollection components = container.Components;
14 ArrayList ret = new ArrayList();
15 foreach (IComponent control in components)
16 {
17 if ( ! (control is Button || control is LinkButton || control is ImageButton))
18 {
19 continue ;
20 }
21 Control button = (Control) control;
22 if ((button.ID != null ) && (button.ID.Length != 0 ))
23 {
24 ret.Add( string .Copy(button.ID));
25 }
26 }
27 ret.Sort(Comparer.Default);
28 return ret.ToArray();
29 }
30
31 public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
32 {
33 if ((context != null ) && (context.Container != null ))
34 {
35 object [] controls = this .GetControls(context.Container);
36 if (controls != null )
37 {
38 return new TypeConverter.StandardValuesCollection(controls);
39 }
40 }
41 return null ;
42 }
43
44 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
45 {
46 return false ;
47 }
48
49 public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
50 {
51 return true ;
52 }
53 }
54 }
2 using System.Web.UI;
3 using System.Web.UI.WebControls;
4 using System.ComponentModel;
5 using System.Collections;
6
7 namespace MyCompany.Controls
8 {
9 public class SubmitableControlConvertor : StringConverter
10 {
11 private object [] GetControls(IContainer container)
12 {
13 ComponentCollection components = container.Components;
14 ArrayList ret = new ArrayList();
15 foreach (IComponent control in components)
16 {
17 if ( ! (control is Button || control is LinkButton || control is ImageButton))
18 {
19 continue ;
20 }
21 Control button = (Control) control;
22 if ((button.ID != null ) && (button.ID.Length != 0 ))
23 {
24 ret.Add( string .Copy(button.ID));
25 }
26 }
27 ret.Sort(Comparer.Default);
28 return ret.ToArray();
29 }
30
31 public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
32 {
33 if ((context != null ) && (context.Container != null ))
34 {
35 object [] controls = this .GetControls(context.Container);
36 if (controls != null )
37 {
38 return new TypeConverter.StandardValuesCollection(controls);
39 }
40 }
41 return null ;
42 }
43
44 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
45 {
46 return false ;
47 }
48
49 public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
50 {
51 return true ;
52 }
53 }
54 }
在页面上只需要象下面这样调用就可以:
1
<%
@ Control Language
=
"
c#
"
%>
2 <% @ Register TagPreFix = " MyCompany " Namespace = " MyCompany.Controls " Assembly = " MyCompany.Controls " %>
3 < MyCompany:SUBMITTEXTBOX id ="txtKeys" SubmitControl ="btnSearch" runat ="server" maxlength ="20" Value ="请输入搜索关键字" onfocus ="if(this.value=='请输入搜索关键字')this.value='';" onblur ="if(this.value=='')this.value='请输入搜索关键字'" ></ MyCompany:SUBMITTEXTBOX > < asp:imagebutton id ="btnSearch" runat ="server" ></ asp:imagebutton >
2 <% @ Register TagPreFix = " MyCompany " Namespace = " MyCompany.Controls " Assembly = " MyCompany.Controls " %>
3 < MyCompany:SUBMITTEXTBOX id ="txtKeys" SubmitControl ="btnSearch" runat ="server" maxlength ="20" Value ="请输入搜索关键字" onfocus ="if(this.value=='请输入搜索关键字')this.value='';" onblur ="if(this.value=='')this.value='请输入搜索关键字'" ></ MyCompany:SUBMITTEXTBOX > < asp:imagebutton id ="btnSearch" runat ="server" ></ asp:imagebutton >