惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
WordPress大学
WordPress大学
Project Zero
Project Zero
P
Palo Alto Networks Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
T
Tailwind CSS Blog
Forbes - Security
Forbes - Security
F
Full Disclosure
SecWiki News
SecWiki News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News: Ask HN
Hacker News: Ask HN
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
O
OpenAI News
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
IT之家
IT之家
U
Unit 42
腾讯CDC
S
Security Affairs
C
Cisco Blogs
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss

博客园 - 分享 共赢

大爱,netbeans的远程开发 lvs + keepalived udp小结 web项目下,甩开RazorTemplateEngine做模板处理 将watin的ui单元测试集成到cc.net 命令行发布web项目 Microsoft © SilverlightTM Release History 针对sl的ICSharpCode.SharpZipLib,只保留zip,gzip的流压缩、解压缩功能 不同IComparer对数组排序Array.Sort,Linq orderby的性能的影响 c# 编译器优化的功劳?与泛型有关的代码的疑惑 高效获取某个数字的最后2位 centos asp.net运行环境配置 扣出MSLinqToSQLGenerator的基类,可用于开发自定义工具(custom tool) [转]我在微软做PM ... Linq to sql 简单性能差异指引 April Rosario(vs2010?) CTP now available! 手工杀毒利器 在form上设定了defaultbutton属性之后,切换提交按钮的解决办法 Bingo Day-展示自我,共享成功! 使用System.Net.Mail发送邮件,vs2005与vs2008存在差别?
在form上设定了defaultbutton属性之后,切换提交按钮的解决办法
分享 共赢 · 2008-04-04 · via 博客园 - 分享 共赢

      通常我们有些页面需要设置默认提交按钮。也可能碰到如下情况:在搜索框中输入搜索内容后,按回车搜索,但由于页面设置了默认按钮,而且默认按钮不是搜索按钮,此时使用如下脚本通常都会失败:

function textBoxOnKeyDown(event)
{
        
if(event.keyCode == 13)
        
{
            __doPostBack(
'Button2','');
        }

}

      而且使用如下脚本也会失败

function textBoxOnKeyDown(event)
{
        
if(event.keyCode == 13)
        
{
            WebForm_FireDefaultButton(event, 
'Button2'); //或在本行前加上return
        }

}

      WebForm_FireDefaultButton是asp.net2的脚本文件里提供的函数,定义如下:

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;
}

       而我测试能工作的脚本如下:

    function textBoxOnKeyDown(event)
  
{
        
if(event.keyCode == 13)
        
{
            WebForm_FireDefaultButton(event, 
'Button2');
            __doPostBack(
'Button2','');
        }

    }

      在xp专业版,ie6上测试通过,完整的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<!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>
    
<script language="javascript" type="text/javascript">
    
function textBoxOnKeyDown(event)
    
{
        
if(event.keyCode == 13)
        
{
            WebForm_FireDefaultButton(event, 
'Button2');
            __doPostBack(
'Button2','');
        }

    }

    
</script>
    
</head>
<body>
    
<form id="form1" runat="server" defaultbutton="Button1">
    
<div>
        
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        
<asp:TextBox ID="TextBox1" runat="server" onkeydown='textBoxOnKeyDown(event)'></asp:TextBox>
        
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
    
</div>
    
</form>
</body>
</html>

       后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication5
{
    
public partial class _Default : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{

        }


        
protected void Button1_Click(object sender, EventArgs e)
        
{
            
this.Response.Write("Button1_Click");
        }


        
protected void Button2_Click(object sender, EventArgs e)
        
{
            
this.Response.Write("Button2_Click");
        }

    }

}