开发者

How to display a custom list in ASP.NET webform

开发者 https://www.devze.com 2023-03-11 14:15 出处:网络
I am working on an ASP.NET webform that functions similar to a blog. On the main form I want to display a list of the recent post titles and under each the first 75 characters of the post text. What w

I am working on an ASP.NET webform that functions similar to a blog. On the main form I want to display a list of the recent post titles and under each the first 75 characters of the post text. What would be a good control to use to display these titles/descriptions? Should I just开发者_JS百科 dynamically add Labels into an on the page or is there a better way?


In WebForms use the <asp:ListView> control. It allows full flexibility for the markup while binding to a data source that provides the model of your blog entries.


I advise you to use the <asp:Repeater> control, and use <li> html elements in it's <ItemTemplate>.


If you want just to displaay some data, without any edit or sorting, use one of the 2 light data bound controls: 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>

or 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>


use html unordered list instead...generate it dynamically and you can use alternating colors for the entires by using jquery/css

In Csharp define a string this way, which would be static

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

the dynamic li for every post.

string dynamic= string.empty;

dynamic += "<li>" + ContentYouExtractFromdatabase + "<hr></li>";

newDynamicLink = mainDiv + dynamic + "</ul></div>";

now keep appending your li's depending upon the post you receive.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号