简体   繁体   English

使用箭头键导航

[英]Navigate using Arrow Keys

I m using this Gridview 我正在使用此Gridview

http://gridviewscroll.aspcity.idv.tw/Demo/Style.aspx#StyleCustom2 http://gridviewscroll.aspcity.idv.tw/Demo/Style.aspx#StyleCustom2

Can someone tell me how i can Navigate into GridView using Arrow Keys into that gridview 有人可以告诉我如何使用箭头键导航到GridView中吗

Thanks :) 谢谢 :)

  1. Edit the webform with the GridView you wanna extend with the functionallity. 使用想要扩展功能的GridView编辑Webform。 There, add two buttons. 在那里,添加两个按钮。 ButtonUp and ButtonDown. ButtonUp和ButtonDown。

  2. Add the following click-events for the buttons. 为按钮添加以下单击事件。 I assume that your GridView is called GridView1: 我假设您的GridView称为GridView1:

      protected void ButtonUp_Click(object sender, EventArgs e) { int i = GridView1.SelectedIndex; if(i>0) GridView1.SelectedIndex = GridView1.SelectedIndex - 1; } protected void ButtonDown_Click(object sender, EventArgs e) { int i = GridView1.SelectedIndex; if (i < GridView1.Rows.Count - 1) GridView1.SelectedIndex = GridView1.SelectedIndex + 1; } 
  3. If you run your page now, you are able to navigate through GridView with the buttons on the page. 如果现在运行页面,则可以使用页面上的按钮浏览GridView。 Now, we will bind the button clicks to our keyboard via javascript. 现在,我们将通过javascript将按钮点击绑定到我们的键盘。 Add the following code to the Page_Load event: 将以下代码添加到Page_Load事件:

      ClientScript.RegisterClientScriptBlock(typeof(string), "keyScript", @"function move(e) { var key = 0; if(window.event) key = event.keyCode; else key = e.keyCode; if(key == 38) document.getElementById('ButtonUp').click(); if(key == 40) document.getElementById('ButtonDown').click(); } document.onkeydown=move; ", true); 
  4. Now you should be able to navigator with your keyboard up and down keys. 现在,您应该可以使用键盘的上下键进行导航了。

  5. To make the page buttons invisible, create the following CssClass for them: 要使页面按钮不可见,请为它们创建以下CssClass:

     .Invisible { display:none; } 
<script type="text/javascript">
    $(document).keydown(function (e) {
        var keyCode = e.keyCode || e.which;
        var arrow = { left: 37, up: 38, right: 39, down: 40 };
        switch (keyCode) {
            case arrow.left:
                break;
            case arrow.up:
                document.getElementById(('<%= ButtonUp.ClientID %>')).click();
                break;
            case arrow.right:
                break;
            case arrow.down:
                //alert("down");
                document.getElementById(('<%= ButtonDown.ClientID %>')).click();
                break;
        }
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM