locked
Change the selected row css from codebehind RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I would like to change the selected row background color after the user clicks the "View Rec." link button.

    <asp:Panel ID="showMessagesPanel" CssClass="ModalWindowQur" style = "display:nonetext-align:centerheight:120%overflow:auto;" runat="server">
                                <div style="max-height400pxoverflowautotext-alignleft;">
                                    <div class="popup_header">
                                        Messages
                                    </div>
                                    <div class="popup_Body" style="padding-left:9%;">
                                        <span>
                                            <asp:GridView ID="messagesGridView" runat="server" DataKeyNames="Memo_Idx" 
                                                AutoGenerateColumns="False" ShowHeader="False" Width="60px" BackColor="#ffffff" 
                                                GridLines="None" OnRowDeleting="gv_MessagesRowDeleting" 
                                                OnSelectedIndexChanged="messagesGridView_SelectedIndexChanged">
                                                <Columns>
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:Panel CssClass="popupMenu" ID="PopupMenu2" runat="server">
                                                                <div style="border1px outset whitepadding2pxz-index:5000">
                                                                    <div>
                                                                        <asp:LinkButton ID="LinkButtonRepsDelete" runat="server" CommandName="Delete" Text="Delete" />
                                                                        <br />
                                                                        <asp:LinkButton ID="LinkButtonRepsSelect" runat="server" CommandName="Select" Text="View Rec." OnClientClick="openMessageForm"/>
                                                                    </div>
                                                                </div>
                                                            </asp:Panel>
                                                            
                                                            
                                                            <asp:Panel ID="Panel10" runat="server">
                                                                <table style="width:700px;  table-layout:fixed;">
                                                                    
                                                                    <tr>
                                                                        
                                                                        <td style="width:5%border-bottom1px solid #ddd;">
                                                                            <asp:Label Font-Bold="true" ID="Label11" runat="server" 
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("Memo_Idx"))) %>' />
                                                                        </td>
                                                                        <td style="width:5%border-bottom1px solid #ddd; ">
                                                                            <asp:Label Font-Bold="false" ID="Label5" runat="server"
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("App_Code"))) %>' />
                                                                        </td>
                                                                        <td style="width:10%border-bottom1px solid #ddd; ">
                                                                            <asp:Label Font-Bold="false" ID="Label6" runat="server"
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("Mem_Rec_Numbr"))) %>' />
                                                                        </td>
                                                                        <td style="width:15%border-bottom1px solid #ddd; ">
                                                                            <asp:Label Font-Bold="false" ID="Label7" runat="server"
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("Mem_From"))) %>' />
                                                                        </td>
                                                                        <td style="width:15%border-bottom1px solid #ddd; ">
                                                                            <asp:Label Font-Bold="false" ID="Label9" runat="server"
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("Mem_To_Date2"))) %>' />
                                                                        </td>
                                                                        <td style="width:40%border-bottom1px solid #ddd; ">
                                                                            <asp:Label Font-Bold="false" ID="Label10" runat="server"
                                                                                Text='<%# HttpUtility.HtmlEncode(Convert.ToString(Eval("Mem_MSG"))) %>' />
                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </asp:Panel>
                                                            <ajaxToolkit:HoverMenuExtender ID="hme12" runat="Server"
                                                                HoverCssClass="popupHover"
                                                                PopupControlID="PopupMenu2"
                                                                PopupPosition="Left"
                                                                TargetControlID="Panel10"
                                                                PopDelay="25" />
                                                        </ItemTemplate>
     
                                                        <EditItemTemplate>
                                                            <!-----Panel for displaying the update and cancel options for selected row------>
                                                            <asp:Panel ID="panel_updateDelete" runat="server"  
                                                            CssClass="popupMenu">
                                                                <div style="border1px outset whitepadding2pxz-index:5000">
                                                                    <asp:LinkButton ID="lnkUpdate"  CommandName="Update" 
                                                                    runat="server" Text="Update"></asp:LinkButton><br />
                                                                    <asp:LinkButton ID="lnkCanel" runat="server" 
                                                                    Text="Cancel" CommandName="Cancel"></asp:LinkButton>
                                                                </div>
                                                            </asp:Panel>
     
                                                             <!-----Panel for displaying the TextBoxes in GridView------>
                                                            <asp:Panel ID="panel_TB" runat="server" Style="width:90%;">
                                                                <table  border="1" style="border-collapse:collapsewidth:100%;">
                                                                    <tr>
                                                                        <td style="width:30%;"><asp:TextBox ID="txtName" style="width:auto;" 
                                                                        Text='<%#Eval("Qur_Name"%>' runat="server">
                                                                        </asp:TextBox></td>
                                                                        <td style="width:70%;"><asp:TextBox ID="txtDesc" style="width:auto;"
                                                                        Text='<%#Eval("Qur_Desc"%>' runat="server">
                                                                        </asp:TextBox></td>
                                                                    </tr>
                                                                </table>
                                                            </asp:Panel>
     
                                                            <!-----HoverMenuExtender for displaying update and cancel options------>
                                                            <ajaxToolkit:HoverMenuExtender ID="hme2" TargetControlID="panel_TB" 
                                                            PopupControlID="panel_updateDelete" PopupPosition="Right" 
                                                            HoverDelay="25" runat="server"></ajaxToolkit:HoverMenuExtender>
                                                        
                                                        </EditItemTemplate>
                                                    </asp:TemplateField>
                                                </Columns>
                                            </asp:GridView>
                                        </span>
                                    </div>
                                    <div class="popup_Buttons" style="padding-left:2%;">
                                        <asp:Button ID="cancelEditButton" runat="server" Text="Done"  />
                                    </div>
                                </div>
                            </asp:Panel>
                            <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender4" runat="server"
                                TargetControlID="NotificationsImageButton"  CancelControlID="cancelEditButton" PopupControlID="showMessagesPanel" DropShadow="true" BackgroundCssClass="modalBackground">
                                <Animations>
                                    <OnShown>
                                        <FadeIn Duration=".4" Fps="20" />                
                                    </OnShown>
                                </Animations>
                            </ajaxToolkit:ModalPopupExtender>
    
    protected void messagesGridView_SelectedIndexChanged(object sender, EventArgs e)
            {//Load the query parameters to the main Queries screen, to allow edit selected fields and conditions
                string mess_id = messagesGridView.SelectedValue.ToString();//get the query id in db
                List<string> app_rec_selected = getApp_field(mess_id, "App_Code, Mem_Rec_Numbr");//get the SQL code for the selected query
                //Response.Redirect(Request.Url.AbsoluteUri + "?TABLE=" + app_rec_selected[0] +
                //                                        "&STATUS=4" +
                //                                        "&ID=" + app_rec_selected[1]);
     
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow",
                                                                        "setTimeout(function(){ var mywin = window.open('DMS_Form.aspx?TABLE=" + app_rec_selected[0] +
                                                        "&STATUS=4" +
                                                        "&ID=" + app_rec_selected[1] +
                                                            
                                                            
                                                                        "','', 'scrollbars=yes, resizable=yes'); if (!mywin) {alert('A popup blocker was detected on the browser, Some functions might not work properly');} else { mywin.focus();}},1000);"true);
     
                messagesGridView.SelectedIndex = -1;
                updateMessageRead(mess_id);//update in the db that the message was read by the user (the user clicked the view rec. button
     
                
            }

    I tried adding the following code but it doesn't seem to work:

    foreach (GridViewRow row in messagesGridView.Rows)             {                 if (row.Cells[0].Text.ToString() == mess_id)                 {                     row.BackColor = System.Drawing.Color.Gray;                 }             }

    Wednesday, May 22, 2019 1:20 PM

Answers

  • User288213138 posted

    Hi  qsoft_developer,
     
    You can change the background color of the selected row using the click event of the LinkButton control.
     
    The code:

    protected void LinkButtonRepsSelect_Click(object sender, EventArgs e)
            {
                GridViewRow row = ((LinkButton)sender).NamingContainer as GridViewRow;
    
                row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 7:34 AM

All replies

  • User-943250815 posted

    You have to use RowDataBound event instead o SelectedindexChange.
    Move your code from SelectedIndexChange to RowDataBound

    Wednesday, May 22, 2019 2:15 PM
  • User288213138 posted

    Hi  qsoft_developer,
     
    You can change the background color of the selected row using the click event of the LinkButton control.
     
    The code:

    protected void LinkButtonRepsSelect_Click(object sender, EventArgs e)
            {
                GridViewRow row = ((LinkButton)sender).NamingContainer as GridViewRow;
    
                row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 23, 2019 7:34 AM