asp.net issue, if you have a bit of spare time

Joined
Mar 4, 2008
Messages
1
Reaction score
0
Hello, I have a problem in ASP.NET, if anyone would be so kind as to look a bit over it and provide a hint I would be much obliged as I tried a lot of time with it but could not find a solution.

I want to make a blog in Asp.Net. I have a Blog class, and this object retrieves its elements from SQL Server. The Blog object contains BlogID, BlogDate, BlogText.

Now, in my BlogMain.aspx page, in its codebehind (BlogMain.aspx.cs) I make a collection, and in it load all those blogs that were inserted in a specific month. So for example my Collection<Blog> will have 2 blog objects for january.

Also, in if( !isPostBack ), I create a Webcontrols.table and populate it dynamically, adding a row for each blog object. So far so good. The problem is that my table disappears on postback, if I put a button anywhere and use it. What can I do to persist it?

Here is my code located in BlogMain.aspx.cs:

Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections.ObjectModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


/// <summary>
///  in this webpage there will be a selection of blogs contained in a specific date; SQL will receive the year
/// and month parameters, and from SQL there will be retrieved those blogs that fit the parameters passed. 
/// then a collection of those blogs will be made and a table will be created to contain all those blogs retrieved from SQL. 
/// </summary>
/// 
public partial class pages_BlogMain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Collection<Blog.BlogComment> colCommentsByPostId = new Collection<Blog.BlogComment>();
            Collection<Blog.BlogPost> colBlogsByMonthYear = new Collection<Blog.BlogPost>();           

            string thisYear = DateTime.Now.Year.ToString();
            string thisMonth = DateTime.Now.Month.ToString();
            colBlogsByMonthYear = Blog.BlogPost.ReadBlogsByMonthYear(thisYear, thisMonth);

            System.Web.UI.WebControls.Table blogsTable = new Table();

            foreach (Blog.BlogPost post in colBlogsByMonthYear)
            {
                int numberOfComments = 0; 

                TableRow rowTitle = Blog.BlogPost.BuildBlogRow(post.BlogPostTitle);
                        rowTitle.CssClass  = "blogTitle";
                        rowTitle.Cells[0].ColumnSpan = 2;
                                tblBlogs.Rows.Add(rowTitle);
                
                TableRow rowText = Blog.BlogPost.BuildBlogRow(post.BlogPostText);
                        rowText.CssClass = "blogText";
                        rowText.Cells[0].ColumnSpan = 2;
                                tblBlogs.Rows.Add(rowText);
                               
                TableRow rowDateComments = new TableRow();
                    TableCell cellDate = new TableCell();
                    TableCell cellComments = new TableCell();
                    
                    rowDateComments.Cells.Add(cellDate);
                    rowDateComments.Cells.Add(cellComments);
                    rowDateComments.CssClass = "blogDate";

                    cellDate.Text = post.BlogPostDate.ToLongDateString() + "  " + post.BlogPostDate.ToShortTimeString();
                    System.Web.UI.WebControls.HyperLink link = new HyperLink();

                    colCommentsByPostId = Blog.BlogComment.ReadCommentsByPostId(post.BlogPostId);
                            for (int i = 0; i < colCommentsByPostId.Count; i++)
                            {
                                numberOfComments++;
                            }
                
                        link.Text = "Comments(" + numberOfComments.ToString() + ")" ;
                        link.NavigateUrl = "http://www.google.com";
                     
                    cellComments.Controls.Add(link);

                    rowDateComments.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                    rowDateComments.Cells[1].HorizontalAlign = HorizontalAlign.Right;
                
                tblBlogs.Rows.Add(rowDateComments);

                TableRow rowBlank = new TableRow();
                        rowBlank.CssClass = "blogBlankRow";
                        tblBlogs.Rows.Add(rowBlank);
            }
        }
    }
}

So as you can see I have put all the code that creates the table, on !isPostBack. Should I actually put it into a method and load that method in !isPostBack and also on postback?

Thank you very much in advance for any suggestion.


/Andrei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top