views:

58

answers:

3

I am trying to call a javascript function which will set forecolor and backcolor of a control when the control is loaded

But this function is not raising.

<ItemTemplate>
      <div onload= "invertColor(this,'<%# Eval("ColorCode") %>')">
           <%# Eval("ColorCode") %>
      </div>
</ItemTemplate>

Here is my javascript

    function invertColor(sender, backColor) {
        alert('hi');
//        alert(backColor.toString());
//        if (backColor != '') {
//        
//            sender.css('background-color', backColor);
//            backColor= backColor.substr(1, 6);
//            foreColor = numberToHex(255 - parseInt(backColor.substr(0, 2), 16))
//            + numberToHex(255 - parseInt(backColor.substr(2, 2), 16))
//            + numberToHex(255 - parseInt(backColor.substr(4, 2), 16));
//            sender.css('color', "#"+foreColor)
//        }
    }
A: 

Yeah, that's not going to work because there's no onload event on a div. Why not just set an appropriate CSS class?

Yellowfog
@Yellowfog: I have to call a javascript function that will be inverting my backcolor and displaying it as a forecolor. So using css i cant call javascript
Shantanu Gupta
you can use inline-style for the div.
Hoque
As Hoque says, you can use an inline-style. Or, if you're using the same colours in multiple places you can use a css class and push the appropriate <style> block out from the server. I can see no point in doing this from the client, unless there's stuff you haven't gone into yet.
Yellowfog
A: 

you can do it immediately after the div if the element has some way to address it, like and id or css class. making the div a server control will produce a unique id for each item in the collection.

<ItemTemplate> 
      <div runat="server" id="dummy"> 
           <%# Eval("ColorCode") %> 
      </div> 
    <script> invertColor('<% =dummy.ClientID %>', '<%# Eval("ColorCode") %>'); </script>
</ItemTemplate> 

here i've changed the first parameter to a string instead of an object. inside invertColor you can use the id string to get a reference to the element however you see fit.

lincolnk
A: 

Using in-line style

<ItemTemplate> 
  <div style= "color:<%# Eval("ColorCode") %>"> 
       <%# Eval("ColorCode") %> 
  </div> 
</ItemTemplate> 

might be an alternative solution.

Hoque