Gridview highlight row and mouseover & mouseout attributes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blacky
    New Member
    • Jan 2009
    • 40

    Gridview highlight row and mouseover & mouseout attributes

    Hi,

    i have a gridview i need to highlight row on mouse over on the row and highlight the row when clicked , this time mouse over on other rows should not happen.
    i have done with mouse over n out and also higglight row on clicking. But dont know how to perform mouse over on other rows should not happen when a row is highlighted.

    This is my code

    Code:
    aspx.cs
    
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    
    
                    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00FF00';");
                    if (e.Row.RowIndex % 2 == 0)
                    { // even
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FF8040';");
                    } // odd
                    else
                    {
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FF8040';");
                    }
    
                    e.Row.Attributes.Add("onclick", "onGridViewRowSelected('" + id.ToString() + "')");
               
            }
           id++;
    
    
        }
    
    aspx page
    
    <script language="javascript" type="text/javascript">
        var gridViewCtlId = '<%=GridView1.ClientID%>';
        var gridViewCtl = null;
        var curSelRow = null;
        function getGridViewControl()
        {
            if (null == gridViewCtl)
            {
                gridViewCtl = document.getElementById(gridViewCtlId);
            }
        }
        
        function onGridViewRowSelected(rowIdx)
        {
            var selRow = getSelectedRow(rowIdx);
            if (curSelRow != null)
            {
                curSelRow.style.backgroundColor = '#ffffff';
            }
            
            if (null != selRow)
            {
                curSelRow = selRow;
                curSelRow.style.backgroundColor = '#ff0022';
            }
        }
        
        function getSelectedRow(rowIdx)
        {
            getGridViewControl();
            if (null != gridViewCtl)
            {
                return gridViewCtl.rows[rowIdx];
            }
            return null;
        }
    </script>			
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcreated="GridView1_RowCreated">
           <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%# Eval("Name")%>
                 </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Age">
                <ItemTemplate>
                    <%# Eval("Age")%>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:BoundField HeaderText="Gender" DataField="Gender"/>
            
              
            </Columns>
    
    </asp:GridView>

    please help me asap...
    thanks in advance
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    This might help

    Code:
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'");
    
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      never mind i didnt read the entire code you already have the done

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well, in this case you only want to apply the JavaScript to the row if the row is a DataRow, if the GridView's selected index is less than 0 (not selected), and perhapse if the GridView's edit index is also less than 0 (not editing).

        So you should change your if statement to:
        Code:
         if (e.Row.RowType == DataControlRowType.DataRow && GridView1.SelectedIndex<0 && GridView1.EditIndex<0)
        -Frinny

        Comment

        Working...