Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

.net datagrid : onclick change highlight row color

I have a data grid with alternating row colors. I want to be able to highlight the entire row when I click on any of the rendered tds within that row.
I am not really sure if it has to be done in this code or the c3 code. Please help.  Thanks.

<asp:datagrid id="DataGrid1" runat="server" CssClass="datatxt" BackColor="#ededee" Cellspacing="1"
gridline="Both" BorderWidth="0" CellPadding="0" Width="100%" AllowSorting="True" AllowPaging="True"
PagerStyle-Mode="NextPrev" PagerStyle-HorizontalAlign="Center" PageSize="25" PagerStyle-NextPageText="Next" PagerStyle-PrevPageText="Prev">
                                          
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F8F8F8"></ItemStyle>
<HeaderStyle Wrap="False" ForeColor="White"></HeaderStyle>
<PagerStyle Visible="False" NextPageText="Next" BorderColor="Transparent" PrevPageText="Prev"
BorderStyle="Dotted" HorizontalAlign="Center" ForeColor="DarkBlue" Position="Top" BackColor="White"
PageButtonCount="20" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
0
doublej31
Asked:
doublej31
  • 2
1 Solution
 
tusharashahCommented:
You can add following code in your ItemDataBound event of DagaGrid to achive what you want:

----------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      switch( e.Item.ItemType )
      {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                  e.Item.Attributes.Add("OnClick","this.style.backgroundColor='LightBlue';");
                  //e.Item.Attributes.Add("OnMouseOver","this.style.backgroundColor='LightBlue';this.style.cursor='hand';");
                  //e.Item.Attributes.Add("OnMouseOut","this.style.backgroundColor ='White'");
            break;
      }
}
----------------------------------------------------------------------------------------------------

-tushar
0
 
doublej31Author Commented:
Thanks that works great, but I need the row that was clicked to go back to its oringinal color when another row is clicked and still be able to highlight in a lighter color
on mouseover.  I hope that made sense.  Thanks
0
 
tusharashahCommented:

Drop a SELECT button Column as 1st Column in your DataGrid and modify your code like following:
--------------------------------------------------------------------------------------------------------
// The BackGround Color of Selected Item is #669999
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  switch( e.Item.ItemType )
  {
        case ListItemType.Item:
        case ListItemType.AlternatingItem:
      ( (LinkButton) e.Item.Cells[7].Controls[0] ).Attributes.Add("OnClick", "return confirm('Are you sure?');" );
                e.Item.Attributes.Add("OnMouseOver","if(this.style.backgroundColor != '#669999')this.style.backgroundColor='#f5f5f5';this.style.cursor='hand';");
      e.Item.Attributes.Add("OnMouseOut","if(this.style.backgroundColor != '#669999')this.style.backgroundColor ='White'");
      string strID = ( (LinkButton) e.Item.Cells[0].Controls[0] ).ClientID.Replace("__","$_") ;
      e.Item.Attributes.Add("OnClick","javascript:__doPostBack('" + strID  + "');" );

        break;
    }
}
--------------------------------------------------------------------------------------------------------
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now