第1种方法:
就是在Asp.net的页面中找到
<form id="form1" runat="server" defaultbutton="Ok">
设置form的属性 defaultbutton=“默认按钮ID”
这个是我在VS2005中式的,不知道2003中是吗
第2种方法:
ASP.NET 中由于一个页面只有 Form ,经常碰到的一个问题是当在一个文本框中按回车后确不知哪个按钮被当成提交按钮提交了,非常郁闷!!!特别是在有用户控件时,搞得都不敢用 Validator 。
今天实在任不住把它搞定,其实也不麻烦。
基本思路:
自己写一个控件继承 TextBox,增加一个 SubmitControl 属性用来指定此文本框上按回车时由哪个按钮来响应。
再做一个 Converter 增加设计时支持,就是在设计时可以用下拉框选择提交按钮。
目前只考虑了 Button、LinkButton和ImageButton作为提交按钮,当然继承它们的控件也可以。
代码很少,就直接贴在这儿了。
ufoTextBox 控件:
public class ufoTextBox : TextBox
{
[TypeConverter(typeof(SubmitableControlConvertor)), DefaultValue(""),Category("Behavior")]
public string SubmitControl
{
get
{
object ret = this.ViewState["SubmitControl"];
if (ret != null)
{
return (string) ret;
}
return string.Empty;
}
set
{
this.ViewState["SubmitControl"] = value;
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if(this.SubmitControl.Length > 0)
{
Control con = FindControl(SubmitControl);
if(con != null)
{
string script = "if(event.keyCode == 13){document.getElementById('" + con.ClientID + "').click();event.returnValue=false;}";
writer.AddAttribute("onkeydown",script);
}
}
}
}
自定义的 Converter,用于设计时支持:
public class SubmitableControlConvertor : StringConverter
{
private object[] GetControls(IContainer container)
{
com%2Fimages%2Fblog%2F202108%2F08%2F0e223d78583f811fd9c0051f5ddf3ef9.gif%3Fx-oss-process%3Dimage%2Fwatermark%2Csize_16%2Ctext_QDUxQ1RP5Y2a5a6i%2Ccolor_FFFFFF%2Ct_100%2Cg_se%2Cx_10%2Cy_10%2Cshadow_90%2Ctype_ZmFuZ3poZW5naGVpdGk%3D%22%2C%22status%22%3A%22done%22%2C%22alt%22%3A%22%22%2C%22percent%22%3A0%2C%22size%22%3A%7B%22width%22%3A11%2C%22height%22%3A16%2C%22naturalWidth%22%3A11%2C%22naturalHeight%22%3A16%7D%2C%22id%22%3A%22lmNNc%22%7D" data-card-type="inline" data-card-key="image" data-card-loading="true" data-card-editable="false"> ComponentCollection components = container.Components;
ArrayList ret = new ArrayList();
com%2Fimages%2Fblog%2F202108%2F08%2F0e223d78583f811fd9c0051f5ddf3ef9.gif%3Fx-oss-process%3Dimage%2Fwatermark%2Csize_16%2Ctext_QDUxQ1RP5Y2a5a6i%2Ccolor_FFFFFF%2Ct_100%2Cg_se%2Cx_10%2Cy_10%2Cshadow_90%2Ctype_ZmFuZ3poZW5naGVpdGk%3D%22%2C%22status%22%3A%22done%22%2C%22alt%22%3A%22%22%2C%22percent%22%3A0%2C%22size%22%3A%7B%22width%22%3A11%2C%22height%22%3A16%2C%22naturalWidth%22%3A11%2C%22naturalHeight%22%3A16%7D%2C%22id%22%3A%22AWy7h%22%7D" data-card-type="inline" data-card-key="image" data-card-loading="true" data-card-editable="false"> foreach (IComponent control in components)
{
if (!(control is Button || control is LinkButton || control is ImageButton))
{
continue;
}
Control button = (Control) control;
if ((button.ID != null) && (button.ID.Length != 0))
{
ret.Add(string.Copy(button.ID));
}
}
ret.Sort(Comparer.Default);
return ret.ToArray();
}
public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if ((context != null) && (context.Container != null))
{
object[] controls = this.GetControls(context.Container);
if (controls != null)
{
return new TypeConverter.StandardValuesCollection(controls);
}
}
return null;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
没几行代码,自己看吧。
第3种方法:
由于asp.net里面的button都是submit类型的,所以在表单内按回车都是触发的第一个按钮的click事件,如果有多个button则无法在回车的时候触发后面的按钮事件。以下方案可通过设置来改变页面的默认按钮。
1.在基页面内的load事件里加入supportDefaultButton()
2.基页面中声明变量:
Protected DefaultButton As Button
3.在基页面中加入函数supportDefaultButton并重载RaisePostBackEvent Private Sub supportDefaultButton()Sub supportDefaultButton()
GetPostBackEventReference(New System.Web.UI.Control)
RegisterStartupScript("DefaultButton", "<script language='javascript' src='/js/DefaultButton.js'></script>")
End Sub
Protected Overloads Overrides Sub RaisePostBackEvent()Sub RaisePostBackEvent(ByVal sourceControl As System.Web.UI.IPostBackEventHandler, ByVal eventArgument As String)
If (DefaultButton Is Nothing OrElse Request("__EVENTTARGET") <> "") Then
MyBase.RaisePostBackEvent(sourceControl, eventArgument)
ElseIf (Not DefaultButton Is Nothing) Then
MyBase.RaisePostBackEvent(DirectCast(DefaultButton, System.Web.UI.IPostBackEventHandler), Nothing)
End If
End Sub
4.DefaultButton.js文件内容:
for(i=0;i<Form1.elements.length;i++)
{
if(Form1.elements[i].type == "submit")
{
fstsubmit = Form1.elements[i]
fstsubmit.attachEvent("onmouseup", changeEvent)
//break; 这一句应该去掉
}
}
function changeEvent()
{
__doPostBack(event.srcElement.id, '')
}
在继承页面的load事件里:
5.DefaultButton = 需要设置为默认按钮的对象名称
即可。