但如果页面上有多个button时,他会默认到第一个button,这样我们就要给他设置一下了.让他变的随心而意.
每个页面都有一个Form标签,这个Form标签里有个DefaultButton这个属性,文章就在这里了.
1,在同一个编辑页面的情况下,(没有ascx控件).
<form id="form1" runat="server" defaultbutton="btnSearch2">
<div>
<asp:TextBox ID="TextBox 1" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch1" runat="server" Text="搜索1" OnClick="OnSearch" />
<asp:Button ID="btnSearch2" runat="server" Text="搜索2" OnClick="OnSearch" />
</div>
</form>
这时,你定义defaultbutton到哪个Button ,就执行那个Button .
2.如果defaultbutton想定义到ascx控件中的Button .
Default.aspx页面(Default.aspx页面里有个WebUserControl.ascx控件).
default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function WebForm_FireDefaultButton(event, target) {
if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
var defaultButton = document.getElementById(target);
if (defaultButton && typeof(defaultButton.click) != "undefined") {
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'WebUserControl1_btnSearch')">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!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>Untitled Page</title>
<script type="text/javascript">
function WebForm_FireDefaultButton(event, target) {
if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) {
var defaultButton = document.getElementById(target);
if (defaultButton && typeof(defaultButton.click) != "undefined") {
defaultButton.click();
event.cancelBubble = true;
if (event.stopPropagation) event.stopPropagation();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'WebUserControl1_btnSearch')">
<div>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</div>
</form>
</body>
</html>
WebUserControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch1" runat="server" Text="搜索1" OnClick="OnSearch" />
<asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="OnSearch" />
<asp:TextBox ID="tbSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch1" runat="server" Text="搜索1" OnClick="OnSearch" />
<asp:Button ID="btnSearch" runat="server" Text="搜索" OnClick="OnSearch" />
3.如果你不想在页面中点击Enter触发事件.
<form id="form1" runat="server" defaultbutton="Button1">
<div>
<asp:Button ID="Button1" runat="server" Style="display:none;" OnClientClick="javascript:return false;" />
<asp:TextBox ID="TextBox 1" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch1" runat="server" Text="搜索1" OnClick="OnSearch" />
<asp:Button ID="btnSearch2" runat="server" Text="搜索2" OnClick="OnSearch" />
</div>
</form>