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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 分享 共赢

大爱,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");
        }

    }

}