【发布时间】:2018-06-15 15:40:24
【问题描述】:
我有一个具有 CSS 和 OnRowDataBound 事件的 asp:gridview。如果我删除我的 OnRowDataBound 事件,CSS 将按照我的意愿运行,但如果我离开 CSS 和 OnRowDataBound 事件,则 CSS 永远不会触发。
谁能帮我解决这两种方法同时触发和运行的问题?
ASP/HTML
<asp:GridView ID="gvEmps" runat="server" CssClass="Grid" OnRowDataBound="gvEmps_OnRowDataBound" >
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Name"> <ItemStyle CssClass="cellOneCellPadding" Width="50%" /></asp:BoundField>
<asp:BoundField DataField="EmpAddress" HeaderText="Address"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
<asp:BoundField DataField="EmpCity" HeaderText="City"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
<asp:BoundField DataField="EmpState" HeaderText="State"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
<asp:BoundField DataField="EmpZip" HeaderText="Zip"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
<asp:BoundField DataField="EmpPhone" HeaderText="Phone"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
</Columns>
</asp:GridView>
C# Method gvEmps_OnRowDataBound....
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell row in e.Row.Cells)
{
if (design1.Any(x => x == e.Row.Cells[0].Text.ToUpper())) { row.CssClass = "des1"; }
else if (design2.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass = "des2"; }
else if (design3.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass = "des3"; }
else { row.CssClass = "defaultview"; };
}
}
CSS
.cellOneCellPadding {
padding-left: 9pt !important;
}
【问题讨论】:
-
请添加webforms标签以帮助人们识别您的问题。
-
dotnetfiddle.net 使用它并创建一个演示
-
如果您检查您希望有 9px 的填充左侧的字段,您会看到
cellOneCellPadding类吗?我的猜测是row.CssClass = "des1"等部分实际上覆盖了类参数,您不再拥有您在 css 中定义的cellOneCellPadding
标签: c# html css asp.net webforms