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