详细内容
GridView选中行效果
发布日期:2010-09-22     点击:4677     字体:[ ]

GridView选中行效果可以在JS里实现,页可以在C#里编程,其实原理都一样,无非就是把其他行显示效果和当前选中行效果不一样即可,请看下面代码:

<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++;
    }
}
 
 当点某行时,直接选中,然后移动方向键则切换不同的选中行; 如果直接按方向键,则从第一行开始标识

用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计