网格视图SelectedRowStyle不适用于模板字段

时间:2011-08-23 09:48:34

标签: c# asp.net

标记

<asp:GridView ID="gridView_Config" runat="server" AutoGenerateColumns="False"
  CssClass="gridViewStyle" OnRowDataBound="gridView_Config_RowDataBound" OnSelectedIndexChanged="gridView_Config_SelectedIndexChanged">
 <Columns>
  <asp:TemplateField ControlStyle-CssClass="ConfigProfileColumn ProfileTextBoxWidth">
 <ItemTemplate>
 <div>
 <asp:TextBox Text='<%# Bind("Type") %>' ID="type" runat="server"  ReadOnly="true" CssClass="FieldLabel " ></asp:TextBox>
</div>                                                        </ItemTemplate>                                                      
</asp:TemplateField>                                                                        </Columns>
 <SelectedRowStyle CssClass="gridViewSelectedRow" />
 <HeaderStyle CssClass="gridHeader" />
</asp:GridView>

代码隐藏

protected void gridView_PrintConfig_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check the item being bound is actually a DataRow, if it is, 
    //wire up the required html events and attach the relevant JavaScripts 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gridView_PrintConfig, "Select$" + e.Row.RowIndex); 
    } 
}

gridViewSelectedRow css类的背景色为蓝色,颜色为白色。但是当选择一行时,模板字段的文本颜色仍然是黑色而不是白色。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您需要在asp:GridView

中添加选择按钮
<asp:CommandField ShowSelectButton="True" Visible="False" />

阅读本文:http://www.codeproject.com/KB/webforms/JavaRowSelect.aspx