Highlighting a row in asp.net gridview on click of template button
我有一个 asp.net 网格视图,我的结构是这样的..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <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> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="175" /> </asp:BoundField> <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" /> </asp:BoundField> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="50" /> </asp:BoundField> <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" /> <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="60" /> </asp:BoundField> <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> |
即使我指定了作为外部 Css 的
我也试过这样做
有人能告诉我为什么这不起作用吗?
从您发布的代码中,您无法选择一行。如果要应用
有几种方法可以选择一行:
-
使用带有
CommandName="Select" 的按钮/图像/链接 -
通过从后面的代码设置网格视图
SelectedIndex 来手动处理它。
例如,在您的行命令处理程序
1 2 3 4 5 6 7 8 | 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 [...] } |