【问题标题】:How to display a custom list in ASP.NET webform如何在 ASP.NET 网络表单中显示自定义列表
【发布时间】:2011-06-08 03:50:24
【问题描述】:

我正在开发一个功能类似于博客的 ASP.NET 网络表单。在主窗体上,我想显示最近帖子标题的列表,并在每个帖子文本的前 75 个字符下显示。什么是显示这些标题/描述的好控件?我应该只是在页面上动态添加标签还是有更好的方法?

【问题讨论】:

  • 改用 html 无序列表...动态生成它,您可以使用 jquery/css 为整体使用交替颜色...
  • @user648372 问题的重点正是关于如何生成ul 元素

标签: asp.net label blogs dynamic-data


【解决方案1】:

在 WebForms 中使用 <asp:ListView> 控件。它在绑定到提供博客条目模型的数据源时为标记提供了充分的灵活性。

【讨论】:

    【解决方案2】:

    我建议你使用<asp:Repeater> 控件,并在它的<ItemTemplate> 中使用<li> html 元素。

    【讨论】:

      【解决方案3】:

      如果您只想显示一些数据,而不进行任何编辑或排序,请使用 2 个轻量数据绑定控件之一:ListView

      <asp:ListView runat="server" ID="ListView1" 
          DataSourceID="SqlDataSource1">
        <LayoutTemplate>
          <table runat="server" id="table1" >
            <tr runat="server" id="itemPlaceholder" ></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td runat="server">
              <%-- Data-bound content. --%>
              <asp:Label ID="NameLabel" runat="server" 
                Text='<%#Eval("Name") %>' />
              <%-- Add 75 chars of text here. --%>
            </td>
          </tr>
        </ItemTemplate>
      </asp:ListView>
      

      Repeater:

      <asp:Repeater runat="server" ID="Repeater1" 
          DataSourceID="SqlDataSource1">
        <HeaderTemplate>
          <table>
            <tr>
              <th>
                Name</th>
              <th>
                Description</th>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
          <tr>
            <td>
              <%-- Data-bound content. --%>
              <asp:Label ID="NameLabel" runat="server" 
                Text='<%#Eval("Name") %>' />
              <%-- Add 75 chars of text here. --%>
            </td>
          </tr>
        </ItemTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
      </asp:Repeater>
      

      【讨论】:

        【解决方案4】:

        改为使用 html 无序列表...动态生成它,您可以使用 jquery/css 为整体使用交替颜色

        在 Csharp 中以这种方式定义一个字符串,这将是静态的

        string mainDiv = "<div class=\"Container\"><ul id=\"BlogPost\">";
        

        每个帖子的动态 li。

        string dynamic= string.empty;
        
        dynamic += "<li>" + ContentYouExtractFromdatabase + "<hr></li>";
        
        newDynamicLink = mainDiv + dynamic + "</ul></div>";
        

        现在继续根据您收到的帖子附加您的 li。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-16
          • 1970-01-01
          • 1970-01-01
          • 2018-10-12
          • 2010-10-30
          相关资源
          最近更新 更多