我想在用代码绑定的GridView上使用方向键,按回车获取该行的索引值在GridView上使用方向键,已经实现问题是按回车时,应该在哪个事件中获取该行的索引值?代码如下:string strID = "";
    string s = "";
    private int _i = 0;
    Get_Value gv = new Get_Value();
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["LoginName"] == null)
            {
                Response.Write("<script language=javascript>alert('请正常登录进入系统!');this.parent.location='logon.aspx';</script>");
            }
            else
            {
                if (Session["DepartmentCode"] != null)
                {
                    s = Session["DepartmentCode"].ToString();
                }
                strID = Request.QueryString["id"];
                Session["Specification"] = strID.Trim();
                Session["S"] = strID.Trim();
                DataTable dt = gv.A_C_Size(strID, s);//方法
                if (dt.Rows.Count == 0)
                {
                    Response.Write("<script language=javascript>alert('不存在该商品!!!');window.close();</script>");
                }
                else
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    GroupRows(GridView1, 1);
                    GroupRows(GridView1, 2, 1);
                    GridView1.Focus();
                    //GridView1.SelectedIndex = 0;
                    //GridView1.Rows[0].Style.Add("backgroundColor", "#339966");
                    //GridView1.Rows[0].Style.Add("color", "#000033");
                }
            }
        }
        else
        {
            //Response.Write(GridView1.SelectedIndex.ToString());
        }
    }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover",
                "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
        }
        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++;
        }
    }    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
        if (row.RowType == DataControlRowType.DataRow)
        {
            Session["ColorCode"] = row.Cells[1].Text.Trim();
            Session["Size"] = row.Cells[2].Text.Trim();
        }
    }    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session["Discount"].ToString() == "")
        {
            Session["Discount"] = "1.00";
        }
        //参与活动的商品算折
        string sql = "";
        double dis = 0;
        //select dbo.uf_GetSaleDiscount('Y010',getdate(),'001926','A32','M','8800736')
        if (Session["Specification"] != null)
        {
            if (Session["ColorCode"] == null)
            {
                Session["ColorCode"] = "";
            }
            if (Session["Size"] == null)
            {
                Session["Size"] = "";
            }
            sql = "select dbo.uf_GetSaleDiscount('" + Session["DepartmentCode"].ToString() + "',getdate(),'" + gv.Get_WareCode(Session["Specification"].ToString()) + "','" + Session["ColorCode"].ToString() + "','" + Session["Size"].ToString() + "','" + Session["VipCardID"].ToString() + "')";
            string str = DB.GetScalarString(sql);
            if (str == "")
            {
                dis = double.Parse(Session["Discount"].ToString());
            }
            else
            {
                dis = double.Parse(str.Trim()) * double.Parse(Session["Discount"].ToString());
            }
            Session["SaleDiscount"] = String.Format("{0:f2}", dis);
        }
        else
        {
            Response.Redirect("~/error.aspx");
        }
        //关闭窗口
        //Response.Write("<script language=javascript>window.close();</script>");
    }    ///   <summary>   
    ///   合并GridView列中相同的行   
    ///   </summary>   
    ///   <param   name="GridView1">GridView对象</param>   
    ///   <param   name="cellNum">需要合并的列</param>
    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }
                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }    ///   <summary>   
    ///   根据条件列合并GridView列中相同的行   
    ///   </summary>   
    ///   <param   name="GridView1">GridView对象</param>   
    ///   <param   name="cellNum">需要合并的列</param>
    ///   <param   name="cellNum2">条件列(根据某条件列还合并)</param>
    public static void GroupRows(GridView GridView1, int cellNum, int cellNum2)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                }                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    }

解决方案 »

  1.   

    还有脚本代码:<script language="javascript" 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 = '#339966';
            }    </script>
      

  2.   

    自己解决了。添加脚本代码:function window_onkeydown() 
            {
                if(event.keyCode == 13)
                {
                    document.getElementById('index').value = currentRowId;
                }
            }服务端代码:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (index.Value != "")
            {
                blnFlag = true;
            }
        }
    改GridView1_SelectedIndexChanging事件为:
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow row;
            if (blnFlag == true)
            {
                row = GridView1.Rows[int.Parse(index.Value)];
                blnFlag = false;
            }
            else
            {
                row = GridView1.Rows[e.NewSelectedIndex];
            }
            if (row.RowType == DataControlRowType.DataRow)
            {
                Session["ColorCode"] = row.Cells[1].Text.Trim();
                Session["Size"] = row.Cells[2].Text.Trim();
            }
        }
      

  3.   

    客户端代码:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="A_Color_Size.aspx.cs" Inherits="A_Color_Size" %><!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>
        <link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" />
        <script language="javascript" 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 = '#339966';
            }
            
                    function window_onkeydown() 
            {
                if(event.keyCode == 13)
                {
                    document.getElementById('index').value = currentRowId;
                }
            }
                
        </script>
    </head>
    <body style="width: 150px; height: 310px;" background="images/agentbg1.gif" onkeydown="return window_onkeydown()" >
        <form id="form1" runat="server" >
        <div style="height:410px;width:300px;">
            <%--<asp:Panel ID="Panel1" runat="server" Height="417px" Style="left: 2px;
                position: relative; top: -30px" Width="304px">--%>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    OnRowDataBound="GridView1_RowDataBound" CssClass="Grid" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="413px" OnRowCommand="GridView1_RowCommand" >
                    <FooterStyle CssClass="GridFooter" />
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="Select_LB" runat="server" CommandName="select" >选择</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="颜色" DataField="颜色" SortExpression="颜色" />
                        <asp:BoundField HeaderText="尺码" DataField="尺码" SortExpression="尺码" />
                        <asp:BoundField HeaderText="库存量" DataField="库存量" SortExpression="库存量" />
                    </Columns>
                    <RowStyle CssClass="Row" />
                    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="Pager" />
                    <HeaderStyle CssClass="HeadingCell" />
                </asp:GridView>
            <%--</asp:Panel>--%></div>
            <input id="index" type="hidden" runat="server" />
            <input id="hd" type="hidden" runat="server" />
        </form>
    </body>
    </html>
    发现个BUG在GridView上点击鼠标左键后,再使用方向键,接着敲回车,回车失效了哪位大侠帮帮找下原因,小弟先谢谢了