?
Solved

.net datagrid :  onclick change highlight row color

Posted on 2005-03-02
6
Medium Priority
?
397 Views
Last Modified: 2008-02-01
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
Comment
Question by:doublej31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 13441916
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 13443383
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
 

Author Comment

by:doublej31
ID: 13445619
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
 
LVL 18

Accepted Solution

by:
tusharashah earned 2000 total points
ID: 13600545

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question