Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
protected void gridproduct_RowCreated(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
       {
           e.Row.TabIndex = -1;
           e.Row.Attributes["window.onload";] = string.Format("javascript:SelectRow(this, {0})";, 0);
           e.Row.Attributes["onclick"] = string.Format("javascript:SelectRow(this, {0});", e.Row.RowIndex);
           e.Row.Attributes["onkeydown"] = "javascript:return SelectSibling(event);";
           e.Row.Attributes["onselectstart";] = "javascript:return false;";

           e.Row.ToolTip = "Click to select row";
           e.Row.Cells[0].Width = Unit.Pixel(35);
           e.Row.Attributes.Add("onkeypress", "javascript:if (event.keyCode == 13) {  __doPostBack('" + gridproduct.UniqueID + "', 'Select$" + e.Row.RowIndex.ToString() + "'); return false; }");


        // e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gridproduct, "Select$" + e.Row.RowIndex);
       }

   }


My java Script

JavaScript
<script type="text/javascript">
        var SelectedRow = null;
        var SelectedRowIndex = null;
        var UpperBound = null;
        var LowerBound = null;

        window.onload = function () {
            UpperBound = parseInt('<%= this.gridproduct.Rows.Count %>') - 1;
        LowerBound = 0;
        SelectedRowIndex = -1;
    }

    function SelectRow(CurrentRow, RowIndex) {
        if (SelectedRow == CurrentRow || RowIndex > UpperBound || RowIndex < LowerBound) return;

        if (SelectedRow != null) {
            SelectedRow.style.backgroundColor = SelectedRow.originalBackgroundColor;
            SelectedRow.style.color = SelectedRow.originalForeColor;
        }

        if (CurrentRow != null) {
            CurrentRow.originalBackgroundColor = CurrentRow.style.backgroundColor;
            CurrentRow.originalForeColor = CurrentRow.style.color;
            CurrentRow.style.backgroundColor = '#DCFC5C';
            CurrentRow.style.color = 'Black';
        }

        SelectedRow = CurrentRow;
        SelectedRowIndex = RowIndex;
        setTimeout("SelectedRow.focus();", 0);
    }

    function SelectSibling(e) {
        var e = e ? e : window.event;
        var KeyCode = e.which ? e.which : e.keyCode;

        if (KeyCode == 40)
            SelectRow(SelectedRow.nextSibling, SelectedRowIndex + 1);
        else if (KeyCode == 38)
            SelectRow(SelectedRow.previousSibling, SelectedRowIndex - 1);

        return false;
    }
    </script>


What I have tried:

I have tried these code its working fine for select row on up/down arrow but it's not firing row command on Enter key press and not select by default first row of gridview
Posted
Updated 7-Mar-16 20:29pm
v2
Comments
Sinisa Hajnal 8-Mar-16 2:28am    
Selecting first row should be done after the control loads (in window.load or document.ready events).

Enter keycode is 13. Add it to key handler and you should be fine.
[no name] 9-Mar-16 0:51am    
Can you give me any example.
Thanks..
Member 10784450 21-Mar-19 10:00am    
on works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900