2013年6月5日 星期三

ASP.NET TextBox onKeyDown網頁中按Enter鍵後指定一個Button並送出

ASP.NET TextBox onKeyDown網頁中按Enter鍵後指定一個Button並送出

在TextBox1下按Enter之後,觸發Script Event onKeyDown,執行window.open到google網頁;另一個觸發.Net ImageButton1_Click,Redirect到www.msn.com.tw。以上觸發Event會跳過Button1


JavaScript:
<script language="javascript" type='text/javascript'>

        function body_onKeyDown() {
            if (window.event.keyCode == 13) {
                alert(event.type);
                document.all.ImageButton1.focus();
                newWindow = window.open("http://www.google.com", "SelectUser", "width=500,height=500");
                newWindow.focus();
            }
        }
    </script>

HTML:
<body onkeydown="body_onKeyDown()">
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:ImageButton ID="ImageButton1" runat="server"
            onclick="ImageButton1_Click" />
    </div>
    </form>
</body>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("http://www.yahoo.com.tw");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("http://www.msn.com.tw");
    }
}

沒有留言:

張貼留言