Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Community,

I have GridView on an ASP .NET Web Form. This GridView is bound to an SQLDataSource. All columns are TemplateField. The GridView is inside an UpdatePanel. One of the Columns is DropDownList the HTML Markup for this DropDownList is as follows:

ASP.NET
<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>


The Code for buildDropDown() Function is:

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;
    }
}


The application is working fine but when I click the update button in the GridView the color of the row changes back to original.

How can I fix this?

What I have tried:

Added a JavaScript Function to the OnClientClick of the Update Button (for testing with one of the Color Coding Schemes):

JavaScript
function Validate(lnkUpdate) {

    var row = lnkUpdate.parentNode.parentNode; 


    row.style.backgroundColor = "rgb(255, 235, 156)";
    row.style.color = "rgb(156, 101, 0)";


    return true;
}

Still the color changes back to original (White background with Black ForeColor)
Posted
Updated 28-Dec-16 19:58pm
Comments
ZurdoDev 28-Dec-16 8:52am    
You have code that is setting the background to white. I'm confused as to what the problem is.
ZohaibRazaTheDProgrammer 28-Dec-16 9:04am    
The row should be painted in default color only when the values in the dropdown are "" or "2016-12". Why the color is changing to default when I click the Update button.

1 solution

I think it is happening because the page posts back. You should handle the RowDataBound event. Inside that event check the data and assign background color to each row as required.
 
Share this answer
 
Comments
ZohaibRazaTheDProgrammer 29-Dec-16 5:07am    
@Tadid Dash I don't want to call RowDataBound from OnClick or OnClientClick because there is already one function. Is there a way I can call RowDataBound inside from Validate(lnkUpdate)?
That event is not called explicitly. Whenever you do a data bind to update the grid, it is automatically fired for each row.
ZohaibRazaTheDProgrammer 30-Dec-16 4:42am    
@Tadit Dash Adding the code to modify the row color in RowDataBound Event is creating another problem. Now I am not able to change the color of the row using the DropDown. Please suggest how I can share the entire code with you, so that you can run it on your local machine and check it?
If you are not able to change the colour from client side, then debug that code. The code which gives you the row is incorrect may be. No I won't be able to run your code now as I don't have my machine now and will be like that for one more week.
ZohaibRazaTheDProgrammer 2-Jan-17 4:36am    
@Tadit Dash when I add a BreakPoint it is taking me through an infinite loop in ScripResource.axd

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900