无法在GridView中使用DataBound的`Style`属性

时间:2012-03-08 12:56:03

标签: c# asp.net gridview styles html

无法DataBound Style属性

Style='<%# Eval("LeftPadding","padding-left:{0}") %>'

完整代码

<asp:TemplateField HeaderText="Report Item" SortExpression="ReportItem">
    <ItemTemplate>
        <asp:Label Style='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
            runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Width="350px" />
</asp:TemplateField>

但我可以使用DataBound其他一些属性,甚至不是标准的HTML属性,如下面的

<asp:Label StyleTemp='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
                runat="server" Text='<%# Eval("Caption") %>'></asp:Label>

Style有什么问题?

1 个答案:

答案 0 :(得分:0)

得到答案:)

<asp:Label Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
                                                            ID="lblReportItem" runat="server" Text='<%# Eval("Caption") %>'></asp:Label>

在没有为style属性提供单引号并使用string.Format

的情况下尝试
Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>