当前位置:编程学习 > C#/ASP.NET >>

问个repeater控件中绑定checkbox选定行改变颜色问题

页面代码如下:
   
<table class="grid1 style3" cellspacing='0' cellpadding='0' rules='all' align='center' border='1' style='border-color:Black ;border-collapse:collapse;'>
    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" EnableViewState="false"><ItemTemplate>
    <tr><td style="font-size:14px;  font-style:inherit; height:30px"  width="80%" colspan="2" align="left"><%#BindItemInfo(Eval("qiItem"))%></td><td width="20%"></td></tr>
    <asp:Repeater ID="Repeater2" runat="server"><ItemTemplate>
    <tr><td width="8%" style="background-color:White;border-right-width:0"></td><td  style="background-color:White; font-size:13px; height:25px; border-left-width:0" width="75%" align="left"><%#Eval("qiItemContent")%></td><td width="20%" style="background-color:White;" align="left">
      <asp:CheckBox  ID="CheckBox1" runat="server" Checked="true"  onclick="SelectedSingle(this)"  /><%#Eval("qiScore")%>分</td>
      </tr>
      
      </ItemTemplate></asp:Repeater>
    </ItemTemplate></asp:Repeater>
    </table>

界面如下:


我想实现一旦某个复选框取消,其repeater2所在行变颜色,可以用js来实现吧?
网上找来的代码:
      <script type="text/javascript" >
        function SelectedSingle(obj) {
            var f = obj.checked;
             var chkColor = "#ff00ff"; //选中后颜色
            var noColor = "#ff0000";  //取消选中后的颜色
            if (!f)

                obj.parentElement.parentElement.style.backgroundColor = chkColor;
           else
               obj.parentElement.parentElement.style.backgroundColor = noColor;

           } 
感觉没有效果,为什么呢?而且我打了obj点,后面并不出现obj.parentElement.parentElement,为什么呢?怎么改呢?哪位能帮助一下,感谢了。 --------------------编程问答-------------------- 1.在Repeater中加一表格或div,改变表格或div的颜色,如下:
<asp:Repeater ID="Repeater2" runat="server" OnItemCommand="Repeater1_ItemCommand"> 
    <ItemTemplate> 
      <table border="0" cellspacing="0" cellpadding="0" width="100%" runat ="server" id="table1"><tr><td width="8%" style="background-color:White;border-right-width:0"></td><td  style="background-color:White; font-size:13px; height:25px; border-left-width:0" width="75%" align="left"><%#Eval("qiItemContent")%></td><td width="20%" style="background-color:White;" align="left">
      <asp:CheckBox  ID="CheckBox1" runat="server" Checked="true"  onclick="SelectedSingle(this)"  /><%#Eval("qiScore")%>分</td>
      </tr></table>
    </ItemTemplate>
</asp:Repeater>

2.在Repeater的ItemDataBound事件中有如下代码,即click调用js:
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    HtmlTable myTable = (HtmlTable)e.Item.FindControl("table1");
    if (myTable != null)
      myTable.Attributes.Add("onclick", "changeColor('" + myTable.ClientID.ToString() + "')");
}
如果没引用,请添加引用:
using System.Web.UI.HtmlControls;

3.js代码:
<script type="text/javascript">
        function changeColor(obj) {
            var myTable = document.getElementsByTagName("table");
           
            for (i = 0; i < myTable.length; i++) {
                if (myTable[i].id == obj)
                    document.getElementById(myTable[i].id).style.backgroundColor = "#ff0000";
                else
                    document.getElementById(myTable[i].id).style.backgroundColor = "#ffffff";
            }
        }
</script> --------------------编程问答-------------------- 感谢你百忙之中的回复,我按照你的代码运行了,可是出现了这个错误,是什么原因呢?
--------------------编程问答--------------------  <asp:CheckBox  ID="CheckBox1" runat="server" Checked="true"  onclick="changeColor(this)" /> 这个地方的onclick事件应该还要保留吧?我已经改成了changeColor --------------------编程问答--------------------  <asp:CheckBox  ID="CheckBox1" runat="server" Checked="true"  onclick="changeColor(this)"  />如果这个onclick去掉,感觉不出错了,可是点击checkbox后,其所在的repeater2的行颜色根本就不变。为什么呢?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,