1

I'm working on developing a solution for my website that will grab headlines and news articles from my database and display the 4 latest Headlines on my homepage with links to the full articles. I created a custom form that displays the information in a format that I would like. I tried creating a method in the code behind that would create one of these objects and populate itself with information from the database. This method would be invoked 4 times to display them in a vertical list. This is not going as smoothly as I thought it would. Does anybody have any ideas on how I could go about doing this. This code has to be dynamic since it will pull open a different article every time to display on the homepage. I'm new to the datagrids so if there is something that I can customize for this please point me in the right direction.

Thanks,

1
  • 2
    can you show us what you tried and what didn't worked Commented Feb 21, 2011 at 5:20

3 Answers 3

2

Hey There, I would do the following.

in the ASP.net Page

    <asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
    <p><%# Eval("PostName") %></p>
    <p><%# String.Format("{0:dd/MM/yyyy hh:mm:ssss}",Eval("PostDate")) %></p>
    <a href="<%# Eval("PostAddress") %>">click here</a>
    </ItemTemplate>
    </asp:ListView>

and in the Code Behind.

public partial class _Default : System.Web.UI.Page
{
    public List<Post> posts;

    protected void Page_Load(object sender, EventArgs e)
    {
        posts = new List<Post>();
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-01"), PostName = "Post1", PostAddress = "www.post.com" });
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-02"), PostName = "Post2", PostAddress = "www.post.com" });
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-03"), PostName = "Post3", PostAddress = "www.post.com" });
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-04"), PostName = "Post4", PostAddress = "www.post.com" });
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-05"), PostName = "Post6", PostAddress = "www.post.com" });
        posts.Add(new Post { PostDate = DateTime.Parse("2011-01-06"), PostName = "Post7", PostAddress = "www.post.com" });

        // Load Posts into Control
        LoadxPosts(4);


    }

    private void LoadxPosts(int xPostNum)
    {

        var postxList = posts.OrderByDescending(x=> x.PostDate).Take(xPostNum);

        ListView1.DataSource = postxList;
        ListView1.DataBind();

    }
}




public class Post
{
    public string PostName { get; set; }
    public DateTime PostDate { get; set; }
    public string PostAddress { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming these headlines are readonly in the form, you should consider a repeater.

Comments

0

I would simply load the posts in the code behind ie:

public List<Post> posts;

protected void Page_Load(object sender, EventArgs e)
{
    posts = new List<Post>();
    posts.Add(new Post { ID = 1, Value = "Post 1" });
    posts.Add(new Post { ID = 2, Value = "Post 2" });
}

Then iterate over the posts in the web form:

<%foreach( Post post in posts) { %>
<p><%= post.Value  %></p>
<% } %>

That avoids having to perform any nasty data binding.

1 Comment

nasty data binding? how about nasty code soup. if your going to do inline code looping/rendering - may as well use dynamic html - e.g a server-side placeholder and render HTML onto that. as least the code is in the code behind, not on the HTML. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.