Asp.Net - Highlight table row

Rick
Rick used Ask the Experts™
on
One can use JavaScript to highlight a table row on mouse over at run time:

      <TR bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';"    
            onMouseOut="this.bgColor='#FFFFFF';">
         <TD>Data 1</td><TD>Data 2</TD>
      </TR>

But, how to do this if the table is being dynamically populated?

      Dim tRow As New TableRow()
      Dim tCell As New TableCell

      tblTable.Rows.Add(tRow)

      tCell.Text = strData1
      tblTable.Rows(0).Cells.Add(tCell)

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Luis PérezSoftware Architect in .Net

Commented:
Dim tRow As New TableRow()
tRow.Attributes.Add("onMouseOver", "this.bgColor='gold'")
tRow.Attributes.Add("onMouseOut", "this.bgColor='#FFFFFF'")

Hope that helps

Author

Commented:
I think I'm in the right track here

tblTable.Rows(0).Attributes.Add("onMouseOver", "this.bgColor='#EFEEC9'")
tblTable.Rows(0).Attributes.Add("onMouseOut", "this.bgColor='#FFFFFF''")
 
The problem now is that the lines above will write this to the broser:
"this.bgColor=&#39;#EFEEC9&#39;"  and "this.bgColor=&#39;#FFFFFF&#39;"
Instead of "this.bgColor='#EFEEC9'" and "this.bgColor='#FFFFFF''"

Author

Commented:
Hi Roland,
I didn't see your post before I wrote my comment above.
Do you know how I can get around that problem?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Luis PérezSoftware Architect in .Net

Commented:
Mmmm.... strange. Try this:

tblTable.Rows(0).Attributes.Add("onMouseOver", Server.HtmlEncode("this.bgColor='#EFEEC9'"))
tblTable.Rows(0).Attributes.Add("onMouseOut", Server.HtmlEncode("this.bgColor='#FFFFFF''"))

If still doesn't work, try "HtmlDecode" instead "HtmlEncode".

Author

Commented:
No luck. When I use HtmlDecode I get this:
onMouseOver="this.bgColor=&#39;#EFEEC9&#39;" onMouseOut="this.bgColor=&#39;#FFFFFF&#39;"

And when I use HtmlEncode I get this:
onMouseOver="this.bgColor=&amp;#39;#EFEEC9&amp;#39;" onMouseOut="this.bgColor=&amp;#39;#FFFFFF&amp;#39;"
Software Architect in .Net
Commented:
I don't know what's the origin of the problem. One possible solution is:

tblTable.Rows(0).Attributes.Add("onMouseOver", "highLightRow(this)")
tblTable.Rows(0).Attributes.Add("onMouseOut", "unHighLightRow(this)")

And in your page's javascript, write both functions:

function highLightRow(row) {
    row.bgColor = '#EFEEC9'; // or better row.style.backgroundColor = ...
}

function unHighLightRow(row) {
    row.bgColor = '#FFFFFF';
}

Hope that helps.

Author

Commented:
I haven't thought about that... Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial