无法对GridView中的“样式”属性进行数据绑定

本文关键字:属性 数据绑定 样式 GridView 中的 | 更新日期: 2023-09-27 18:27:04

无法对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有什么问题

无法对GridView中的“样式”属性进行数据绑定

得到答案:)

<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") %>