在单击模板按钮时,在asp.net gridview中突出显示一行

本文关键字:显示 一行 gridview 单击 按钮 asp net | 更新日期: 2023-09-27 18:19:24

我有一个asp.net网格视图,我有这样的结构…

<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">
                <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"
                                CommandArgument="<%#Bind('DeviceID') %>" />
                        </ItemTemplate>
                        <HeaderStyle Width="50" />
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>
                </Columns>
                <RowStyle CssClass="RowStyle" />
                <EmptyDataRowStyle CssClass="EmptyRowStyle" />
                <PagerStyle CssClass="PagerStyle" />
                <SelectedRowStyle CssClass="SelectedRowStyle" />
                <HeaderStyle CssClass="HeaderStyle" />
                <EditRowStyle CssClass="EditRowStyle" />
                <AlternatingRowStyle CssClass="AltRowStyle" />
            </asp:GridView>

即使我已经指定了<SelectedRowStyle CssClass="SelectedRowStyle">,这是一个外部Css,我不能看到所选行背景颜色的变化

我也试过做这个<SelectedRowStyle BackColor="AliceBlue" />,仍然没有变化谁能告诉我为什么不行?

在单击模板按钮时,在asp.net gridview中突出显示一行

从您发布的代码中,您无处选择一行。如果你想要应用SelectedRowStyle,你必须这样做。

有几种方法可以选择行:

  • 使用按钮/图像/链接CommandName="Select"
  • 通过设置栅格视图SelectedIndex从后面的代码手动处理它。

例如,在您的行命令处理程序gridContractor_RowCommand中,您可以写入:

protected void gridContractor_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    [...]
    GridViewRow selectedRow = (GridViewRow((Button)e.CommandSource).NamingContainer;
    int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);
    gridContractor.SelectedIndex = intRowIndex 
    [...]
}