[英]How to call javascript function in item template when using gridview
I am trying to call a javascript function which will set forecolor and backcolor of a control when the control is loaded 我正在尝试调用一个javascript函数,该函数会在加载控件时设置控件的前色和背景色
But this function is not raising. 但是这个功能没有提高。
<ItemTemplate>
<div onload= "invertColor(this,'<%# Eval("ColorCode") %>')">
<%# Eval("ColorCode") %>
</div>
</ItemTemplate>
Here is my javascript 这是我的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)
// }
}
you can do it immediately after the div if the element has some way to address it, like and id or css class. 您可以在div之后立即执行此操作,前提是该元素具有某种寻址方式,例如id和css类。 making the div a server control will produce a unique id for each item in the collection.
将div设置为服务器控件将为集合中的每个项目生成唯一的ID。
<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. 在
invertColor
内部,您可以使用id字符串获取对该元素的引用,但是您认为合适。
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(" + "'" + Eval("EmpId") + "');" %> ' />
</ItemTemplate>
</asp:TemplateField>
Yeah, that's not going to work because there's no onload event on a div. 是的,那是行不通的,因为div上没有onload事件。 Why not just set an appropriate CSS class?
为什么不只设置适当的CSS类呢?
Using in-line style 使用内联样式
<ItemTemplate>
<div style= "color:<%# Eval("ColorCode") %>">
<%# Eval("ColorCode") %>
</div>
</ItemTemplate>
might be an alternative solution. 可能是替代解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.