GridView中使用方向键标记选中行

添加人:iyond七级(4343分)   添加时间:2007-06-18    阅读次数:2031  收藏此教程

在aspx中定义javascript事件,判断按键是否为上下键;

<script type="text/javascript">
        var currentRowId = 0;
        function SelectRow()
        {
            if (event.keyCode == 40)
                MarkRow(currentRowId+1);
            else if (event.keyCode == 38)
                MarkRow(currentRowId-1);
        }

        function MarkRow(rowId)
        {
            if (document.getElementById(rowId) == null)
                return;

            if (document.getElementById(currentRowId) != null )
                document.getElementById(currentRowId).style.backgroundColor = '#ffffff';

            currentRowId = rowId;
            document.getElementById(rowId).style.backgroundColor = '#ff0000';
        }
    </script>


然后在gridview的rowDataBound中, 添加处理按键的事件处理函数和使用鼠标点击某行时的选中事件.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("id", _i.ToString());
        e.Row.Attributes.Add("onKeyDown", "SelectRow();");
        e.Row.Attributes.Add("onClick", "MarkRow(" + _i.ToString() + ");");

        _i++;
    }
}

 
 当点某行时,直接选中,然后移动方向键则切换不同的选中行; 如果直接按方向键,则从第一行开始标识.

 

1页 第1上一页1下一页
相关的教程: GridView 方向键标记选中行
收藏此教程

当前平均分: 0.0(0 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: