2

嗨,我正在尝试更改 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;
    }
4

7 回答 7

4

GridViewRow is a control. Like every object on the page, it will be created during the page life cycle.
The reference you hold in Session is to the object created during the last request.

To solve the problem, keep only the index(or key) of the row in Session and use that to change the color of previous row.

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;
}
于 2013-06-24T06:45:16.677 回答
4
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridView1.SelectedRow.BackColor = System.Drawing.Color.White;
}
于 2013-06-24T14:12:05.307 回答
1

试试下面的东西。

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'");
    }
}
于 2013-06-24T06:50:33.900 回答
1

我一直在寻找解决此问题的方法,然后看到几个类似这样的线程提到您需要跟踪会话中选定的行。虽然这对于部分回发可能是正确的,但我找到了使用 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;
    }
于 2013-11-24T21:57:32.090 回答
0

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

希望这可以帮助,

于 2013-06-24T07:02:51.923 回答
0

使用 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
于 2013-08-16T15:01:45.460 回答
0
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;
        }
}

干杯

于 2021-06-17T02:32:00.457 回答