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