Highlight the datalist row

Discussion in 'ASP .Net Web Controls' started by Lubomir, Mar 28, 2005.

  1. Lubomir

    Lubomir Guest

    Hi,

    I have a datalist with 3 columns. First one contains an image. All of them
    are hyperlinks. Datalist is generated by using ExtractTemplateRows="True".

    My question is how to highlight the entire row by dragging mouse cursor over
    the row.

    I would like to use a script for onMouseOver, but the asp:TableRow doesn’t
    support this event.
    If I use the style for an Item, just the tab-cell with a mouse cursor will
    be highlighted, and not the entire row.

    Any suggestions?

    Thank you for help.

    Lubomir
    Lubomir, Mar 28, 2005
    #1
    1. Advertising

  2. Lubomir

    AlanM Guest

    I have no knowledge of non-IE browsers, so maybe this helps, maybe it
    doesn't:

    Although you can't solve this with an onMouseOver in the row, you can
    change the style of the row that contains the cell from the onMouseOver
    of the cell, like so:

    Use Page.RegisterClientScriptBlock to squirt something like this into
    the page:
    <script language="javascript">
    function SetRowYellow(td)
    {
    var tr = td.parentElement;
    tr.style.backgroundColor = "yellow";
    }
    function SetRowWhite(td)
    {
    var tr = td.parentElement;
    tr.style.backgroundColor = "white";
    }
    </script>

    Then, for each asp:TableCell, you'll need to add onmouseover and
    onmouseout event handlers that calls those functions. I find it easiest
    to do for all cells in the DataGrid in the ItemCreateed event:

    private void DataGrid1_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    switch (e.Item.ItemType)
    {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    case ListItemType.SelectedItem:
    foreach (TableCell cell in e.Item.Cells)
    {
    cell.Attributes.Add("onmouseover", "SetRowYellow(this);");
    cell.Attributes.Add("onmouseout", "SetRowWhite(this);");
    }
    break;
    }
    }

    This adds a little bit of bloat to the page, but it does work.

    ->AlanM
    AlanM, Mar 29, 2005
    #2
    1. Advertising

  3. Lubomir

    AlanM Guest

    BTW - I know this isn't exactly the same as a DataList with
    ExtractTemplateRows=True, but the basic idea should still work.
    ->AlanM
    AlanM, Mar 29, 2005
    #3
  4. Lubomir

    AlanM Guest

    turns out it needs a lot more for ExtractTemplateRows. Here's a new
    ItemCreated event handler:

    private void DataList1_ItemCreated(object sender,
    System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    switch (e.Item.ItemType)
    {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
    case ListItemType.SelectedItem:
    foreach (Control c in e.Item.Controls)
    {
    if (c is Table)
    {
    Table table = (Table)c;
    foreach (TableRow row in table.Rows)
    {
    foreach (TableCell cell in row.Cells)
    {
    cell.Attributes.Add("onmouseover",
    "SetRowYellow(this);");
    cell.Attributes.Add("onmouseout",
    "SetRowWhite(this);");
    }
    }
    }
    }
    break;
    }
    }
    AlanM, Mar 29, 2005
    #4
  5. Lubomir

    Lubomir Guest

    Hi Alan,

    Thank you :)

    Lubomir



    "AlanM" wrote:

    > turns out it needs a lot more for ExtractTemplateRows. Here's a new
    > ItemCreated event handler:
    >
    > private void DataList1_ItemCreated(object sender,
    > System.Web.UI.WebControls.DataListItemEventArgs e)
    > {
    > switch (e.Item.ItemType)
    > {
    > case ListItemType.Item:
    > case ListItemType.AlternatingItem:
    > case ListItemType.SelectedItem:
    > foreach (Control c in e.Item.Controls)
    > {
    > if (c is Table)
    > {
    > Table table = (Table)c;
    > foreach (TableRow row in table.Rows)
    > {
    > foreach (TableCell cell in row.Cells)
    > {
    > cell.Attributes.Add("onmouseover",
    > "SetRowYellow(this);");
    > cell.Attributes.Add("onmouseout",
    > "SetRowWhite(this);");
    > }
    > }
    > }
    > }
    > break;
    > }
    > }
    >
    >
    Lubomir, Mar 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,869
  2. =?Utf-8?B?Q2hyaXMgRmluaw==?=

    Highlight datalist row on itemDataBound event

    =?Utf-8?B?Q2hyaXMgRmluaw==?=, Dec 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,097
    rcbalbo
    Aug 9, 2006
  3. Fernando Lopes
    Replies:
    0
    Views:
    3,192
    Fernando Lopes
    Apr 28, 2005
  4. H5N1
    Replies:
    0
    Views:
    2,716
  5. D
    Replies:
    0
    Views:
    207
Loading...

Share This Page