Datagrid Mailbag: Changing row's cssclass on the client

Don't tell anyone, but I do sometimes answer Datagrid questions sent by private email.  (Note:  I don't have time to answer ALL the questions I receive, so I apologize if your question didn't receive a response).  So I thought I would share here some of the answers that I give.

Today's emailer wants to know:  How do I change the CssClass of a Datagrid row after the user has clicked on a HyperLink control in that row?

Well, I worked up a sample on how to do this using a Javascript function (and if anyone has a better way, please let me know).  Let's say the relevant Datagrid column looks like this:

    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Hyperlink id="docURL" runat="server" NavigateUrl=<%#"Javascript:ShowDoc(" & Container.ItemIndex & ");"%>>Doc Name</asp:Hyperlink>
    </ItemTemplate>
    </asp:TemplateColumn>

The Javascript I came up with (using the current row index in the grid):

   function ShowDoc(obj) {
    window.open('newpage.aspx', 'name')
tabs = document.getElementsByTagName("table");
tabs[0].rows[obj+1].className='ClickedClass';

    }

Published Tuesday, September 16, 2003 4:36 PM by datagridgirl

Comments

# re: Datagrid Mailbag: Changing row's cssclass on the client

You should add this to the DataGridGirl.com Web site FAQ list, if you haven't already.

Cheers.

Wednesday, September 17, 2003 1:37 AM by Scott Mitchell

# re: Datagrid Mailbag: Changing row's cssclass on the client

Good idea, Scott, I think I will.

Wednesday, September 17, 2003 10:52 AM by Datagrid Girl

# re: Datagrid Mailbag: Changing row's cssclass on the client

Are you not setting the className on the first table element in the page - which is not necessarily your DataGrid?

Another approach might be:

<asp:HyperLink onclick='javascript:ShowDoc(this);' NavigateUrl='javascript:void(0);' ...

and
function ShowDoc(src)
{
// A -> TD -> TR
src.parentNode.parentNode.className = 'ClickedClass';
}

Wednesday, September 17, 2003 5:04 PM by Me

# re: Datagrid Mailbag: Changing row's cssclass on the client

Great suggestion! "parentNode" makes that much cleaner--thanks.

Wednesday, September 17, 2003 6:15 PM by Datagrid Girl

# re: Datagrid Mailbag: Changing row's cssclass on the client

i think it can help Thanks

Saturday, September 06, 2008 1:38 AM by Rajesh

Leave a Comment

(required) 
(required) 
(optional)
(required)