1

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)
//        }
    }

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

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
you can use inline-style for the div.
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.
0

Using in-line style

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

might be an alternative solution.

Comments

0

You can also try like this.

<asp:TemplateField HeaderText="Link"> 
 <ItemTemplate> 
    <asp:HyperLink runat="server" ID="HLink" 
        Text='<%# Eval("FirstName").ToString() + " " + Eval("LastName").ToString()%>' 
        NavigateUrl='<%# "javascript:OpenPopup(" + "&#39;" + Eval("EmpId") + "&#39;);" %> ' />
 </ItemTemplate> 
</asp:TemplateField>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.