【问题标题】:How can <td> and <tr> tags inside an ItemTemplate not be nested inside a table?ItemTemplate 中的 <td> 和 <tr> 标签如何不嵌套在表格中?
【发布时间】:2023-03-09 12:05:01
【问题描述】:

我读到here 说 td 标记必须嵌套,所以当我看到 ListView 的 ItemTemplate 属性标记看起来像这样时,我感到很困惑:

<ItemTemplate>    
                            <td id="Td2" runat="server">      
                                <table>        
                                    <tr>          
                                        <td>&nbsp;</td>          
                                        <td>
                                            <a href="ProductDetails.aspx?productID=<%#:Item.ProductID%>">
                                                <img src="/Catalog/Images/Thumbs/<%#:Item.ImagePath%>" 
                                                    width="100" height="75" /></a> 
                                        </td>
                                        <td>
                                            <a href="ProductDetails.aspx?productID=<%#:Item.ProductID%>">
                                                <span class="ProductName">
                                                    <%#:Item.ProductName%>
                                                </span>
                                            </a>            
                                            <br />
                                            <span class="ProductPrice">           
                                                <b>Price: </b><%#:String.Format("{0:c}", Item.UnitPrice)%>
                                            </span>
                                            <br />            
                                        </td>        
                                    </tr>      
                                </table>    
                            </td>  
                        </ItemTemplate>  

这是否与 ListView 从某个类继承并实现大量接口这一事实有关?还是与我不熟悉的 ItemTemplate 的某些方面有关?我的来源是错误的还是“只是一个 ASP 的东西”?

【问题讨论】:

    标签: html asp.net html-table


    【解决方案1】:

    我不知道您的 LayoutTemplate 是什么样的,但这实际上与 ListView 本身无关,而更多地与开发人员选择的 HTML 布局有关。 td 最好属于 tr,但这些只是标签。您可以更改 css 并根据需要重新定义。然后浏览器会解释它并显示它,然后你就可以处理你选择的后果 =)。

    上述代码有意义的一种可能方式是:

    <LayoutTemplate>
        <table border="0" cellpadding="1">
            <tr>
                <th>Product</th>
            </tr>
            <tr>
                <td id="itemPlaceholder" runat="server"></td>
            </tr>
        </table>
    </LayoutTemplate>
    

    这将创建一个带有产品标题的单列外部表,然后该表的每一行将包含您上面的内部表(每个产品)。

    【讨论】:

      【解决方案2】:

      这正是 asp.net 让您定义控件的方式。一旦 asp.net 页面被渲染(转换为纯 html),那些td 元素应该嵌套在tr 中。您可以通过在浏览器中查看页面源来查看输出。

      【讨论】:

        猜你喜欢
        • 2011-05-16
        • 2016-02-26
        • 1970-01-01
        • 2015-02-11
        • 1970-01-01
        • 2014-10-09
        • 2013-03-23
        • 2021-01-11
        • 1970-01-01
        相关资源
        最近更新 更多