【问题标题】:Changing color of selected row in asp.net gridview在asp.net gridview中更改选定行的颜色
【发布时间】:2013-06-20 14:26:44
【问题描述】:

嗨,我正在尝试更改 asp.net gridview 中所选行的颜色,我已经像这样定义了我的网格视图

<asp:GridView ID="gridContractor" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None" EnableModelValidation="True"
                DataKeyNames="DeviceID" OnRowCommand="gridContractor_RowCommand" OnPageIndexChanging="gridContractor_PageIndexChanging"
                Width="100%" EmptyDataText = "No records to display" EmptyDataRowStyle-HorizontalAlign="Center">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField HeaderText="Device IMEI" DataField="DeviceID" Visible="false">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="175" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="Person Name" DataField="PersonName">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="#Observations" DataField="GpsPointsCount" ControlStyle-Width="50px">
                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="50" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="#Violations" DataField="ViolationCount" ControlStyle-Width="60px">
                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="60" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" Text="View" CommandName="View" Enabled="true" OnClick="btn_GrdClick"
                                CommandArgument="<%#Bind('DeviceID') %>" />
                        </ItemTemplate>
                        <HeaderStyle Width="50" />
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>
                </Columns>
                <RowStyle CssClass="RowStyle" />
                <EmptyDataRowStyle CssClass="EmptyRowStyle" />
                <PagerStyle CssClass="PagerStyle" />
                <SelectedRowStyle BackColor="AliceBlue" />
                <HeaderStyle CssClass="HeaderStyle" />
                <EditRowStyle CssClass="EditRowStyle" />
                <AlternatingRowStyle CssClass="AltRowStyle" />
            </asp:GridView>

并且我已经处理了按钮单击事件,问题是每次我选择一行时,即使我正在这样做,前一行的颜色也不会改变。一旦点击该行仍然是黄色的,有人可以帮我吗

我的 aspx.cs 代码

 protected void btn_GrdClick(object sender, EventArgs e)
    {
        GridViewRow PreviousRow = Session["PreviousRow"] as GridViewRow;
        if (PreviousRow != null)
            PreviousRow.ForeColor = Color.White;
        GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
        row.ForeColor = Color.Yellow;
        Session["PreviousRow"] = row;
    }

【问题讨论】:

    标签: c# asp.net gridview webforms


    【解决方案1】:
    protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if (e.CommandName == "xxx")
            {
                foreach (GridViewRow rx in gridName.Rows)
                {
                    rx.ForeColor = Color.White;
                }
    
                ((System.Web.UI.WebControls.TableRow)((System.Web.UI.Control)e.CommandSource).DataItemContainer).ForeColor = Color.Yellow;
            }
    }
    

    干杯

    【讨论】:

      【解决方案2】:

      我一直在寻找解决此问题的方法,然后看到几个类似这样的线程提到您需要跟踪会话中选定的行。虽然这对于部分回发可能是正确的,但我找到了使用 Css 的更好解决方案。

      Gridview 在一个更新面板中,周围的 div 有一个 class=grid,gridview 有 class=datatable 然后在 Gridview 中定义了以下元素;

      RowStyle CssClass="item-row"

      SelectedRowStyle CssClass="selectedItem-row"

      那么关联的css是这样的;

          .grid .datatable .item-row TD {
              border-bottom: #bbd9ee 1px solid;
              text-align: left;
              padding-bottom: 6px;
              padding-left: 4px;
              padding-right: 4px;
              font-size: 0.7em;
              border-top: #bbd9ee 1px solid;
              padding-top: 6px;
          }
      
              .grid .datatable .item-row TD.highlightcell {
                  background-color: #fffacd;
                  color: #000;
              }
      
          .grid .datatable .item-row:hover {
              background-color: #fffacd;
              color: #000;
          }
      
          .grid .datatable .selectedItem-row TD {
              border-bottom: #bbd9ee 1px solid;
              text-align: left;
              padding-bottom: 6px;
              padding-left: 4px;
              padding-right: 4px;
              font-size: 0.7em;
              border-top: #bbd9ee 1px solid;
              padding-top: 6px;
              background-color: #d7ffcd;
          }
      

      【讨论】:

        【解决方案3】:

        使用 GridView 加载事件

        我已经给出了我的代码示例,

        Protected Sub grvaddbook_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles grvaddbook.Load
            Dim row1 As GridViewRow
            row1 = grvaddbook.HeaderRow
            Dim i As Integer
            i = 0
            For Each row As GridViewRow In grvaddbook.Rows
                Dim main As Label = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lblismain"), Label)
        
                If main.Text = 1 Then
                    Dim lbtnmakedefault As LinkButton = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lbtnmakedefault"), LinkButton)
                    lbtnmakedefault.Visible = False
                    mainaid = DirectCast(grvaddbook.Rows(i).Cells(0).FindControl("lblaid"), Label).Text
                End If
                i = i + 1
            Next
        End Sub
        

        【讨论】:

          【解决方案4】:
          protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
          {
              GridView1.SelectedRow.BackColor = System.Drawing.Color.White;
          }
          

          【讨论】:

            【解决方案5】:

            1) 您需要做的第一件事是处理 Grid 的 RowCommand 事件,protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)。您正在做的是将按钮的单击事件与 gridview 的命令事件分开处理。 2)您可以为命令参数分配索引,并更改行的颜色并将所有其他行设置为默认颜色。一个例子可以找到here

            希望对你有帮助,

            【讨论】:

              【解决方案6】:

              GridViewRow 是一个控件。与页面上的每个对象一样,它将在页面生命周期中创建。
              您在Session 中持有的引用是对上次请求期间创建的对象。

              为了解决这个问题,只保留Session中行的索引(或键),并用它来改变前一行的颜色。

              protected void btn_GrdClick(object sender, EventArgs e)
              {
                  if(Session["PreviousRowIndex"] != null)
                  {
                    var previousRowIndex = (int)Session["PreviousRowIndex"];
                    GridViewRow PreviousRow = MyGridView.Rows[previousRowIndex];
                    PreviousRow.ForeColor = Color.White;
                  }
              
                  GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
                  row.ForeColor = Color.Yellow;
                  Session["PreviousRowIndex"] = row.RowIndex;
              }
              

              【讨论】:

                【解决方案7】:

                试试下面的方法。

                protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.backgroundColor='yellow'");
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white'");
                    }
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2022-08-13
                  • 2017-02-13
                  • 1970-01-01
                  • 2018-04-12
                  • 2013-08-13
                  • 2022-01-17
                  • 1970-01-01
                  相关资源
                  最近更新 更多