为什么Gridview backgroundcolor属性不会保留在模板字段中单击链接按钮

时间:2016-12-29 06:26:42

标签: javascript asp.net gridview updatepanel templatefield

我在ASP .NET Web窗体上有 GridView 。此GridView绑定到 SQLDataSource 。所有列都是 TemplateField 。 GridView位于 UpdatePanel 中。其中一列是 DropDownList 。此DropDownList的HTML如下:

<asp:DropDownList ID="DropDownList1" onchange="buildDropDown(this)" runat="server" Text='<%# Bind("[r_cal_M]") %>'>
    <asp:ListItem></asp:ListItem>
    <asp:ListItem>2016-09</asp:ListItem>
    <asp:ListItem>2016-10</asp:ListItem>
    <asp:ListItem>2016-11</asp:ListItem>
    <asp:ListItem>2016-12</asp:ListItem>
</asp:DropDownList>

buildDropDown()函数的JavaScript是:

    function buildDropDown(obj) {
    var status = obj.options[obj.selectedIndex].value;
    var row = obj.parentNode.parentNode;
    var rowIndex = row.rowIndex - 1;

    var ddlReason = row.cells[1].getElementsByTagName('SELECT')[0];

    switch (status) {
        case "":
            row.style.backgroundColor = "rgb(255, 255, 255)";
            row.style.color = "rgb(0, 0, 0)";
            break;
        case "2016-09":
            row.style.backgroundColor = "rgb(198, 239, 206)";
            row.style.color = "rgb(0, 97, 0)";
            break;
        case "2016-10":
            row.style.backgroundColor = "rgb(1255, 199, 206)";
            row.style.color = "rgb(156, 0, 6)";
            break;
        case "2016-11":
            row.style.backgroundColor = "rgb(255, 235, 156)";
            row.style.color = "rgb(156, 101, 0)";
            break;
        case "2016-12":
            row.style.backgroundColor = "rgb(255, 255, 255)";
            row.style.color = "rgb(0, 0, 0)";
            break;
    }
}

应用程序工作正常但当我点击GridView中的更新按钮时,行的颜色会将更改回原始。< / p>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个工作正常

<script type="text/javascript">
         function buildDropDown(obj) {

    var status = obj.options[obj.selectedIndex].value;
    var row = obj.parentNode.parentNode;
    var rowIndex = row.rowIndex - 1;

    switch (status) {
        case "":
            row.parentNode.parentNode.style.backgroundColor = "rgb(255, 255, 255)";
            row.parentNode.parentNode.style.color = "rgb(0, 0, 0)";
            break;
        case "2016-09":
            row.parentNode.parentNode.style.backgroundColor = "rgb(198, 239, 206)";
            row.parentNode.parentNode.style.color = "rgb(0, 97, 0)"
            break;
        case "2016-10":
            row.parentNode.parentNode.style.backgroundColor = "rgb(1255, 199, 206)";
            row.parentNode.parentNode.style.color = "rgb(156, 0, 6)";
            break;
        case "2016-11":
            row.parentNode.parentNode.style.backgroundColor = "rgb(255, 235, 156)";
            row.parentNode.parentNode.style.color = "rgb(156, 101, 0)";
            break;
        case "2016-12":
            row.parentNode.parentNode.style.backgroundColor = "rgb(255, 255, 255)";
            row.parentNode.parentNode.style.color = "rgb(0, 0, 0)";
            break;
    }
   }
 </script>