【问题标题】:Not able to DataBound the `Style` attribute in GridView无法对 GridView 中的“样式”属性进行数据绑定
【发布时间】:2012-03-08 12:56:03
【问题描述】:

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

【问题讨论】:

    标签: c# asp.net gridview styles html


    【解决方案1】:

    得到答案:)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2014-08-15
      • 1970-01-01
      • 2013-09-11
      相关资源
      最近更新 更多