Tuesday, August 26, 2008

Create a Html table dynamically by c# Code

private void LoadForumItems(int mainForumId)
{
ForumItem objForumItems = new ForumItem();
DataSet dsForumItems = objForumItems.GetForumItemsByForumId(mainForumId);

int MainForumId = 0;
int x = 1;
//int j = 1;
HtmlTable htmlMainTable = new HtmlTable();
TrForumItems.Cells[0].Controls.Add(htmlMainTable);
htmlMainTable.Attributes.Add("width", "100%");

//if check the ds empty here
foreach (DataRow dr in dsForumItems.Tables[0].Rows)
{
if (MainForumId != Convert.ToInt32(Request.QueryString["MainForumId"]))
{
HtmlTableRow htmlForumItemRow = new HtmlTableRow();

//Empty Left Cell
HtmlTableCell htmlForumItemEmptyCellLeft = new HtmlTableCell();
htmlForumItemEmptyCellLeft.InnerHtml = " ";
htmlForumItemEmptyCellLeft.Attributes.Add("width", "10");
htmlForumItemEmptyCellLeft.Attributes.Add("class", "tableItems");
htmlForumItemRow.Cells.Add(htmlForumItemEmptyCellLeft);

//Description Cell
HtmlTableCell htmlForumItemCellDescription = new HtmlTableCell();
htmlForumItemCellDescription.InnerHtml = dr["Description"].ToString();
htmlForumItemCellDescription.Attributes.Add("width", "400");
htmlForumItemCellDescription.Attributes.Add("class", "tableItems");
htmlForumItemRow.Cells.Add(htmlForumItemCellDescription);

//Cell reply button
HtmlTableCell htmlForumItemCellButton = new HtmlTableCell();
HyperLink link = new HyperLink();
link.CssClass = "";
link.ToolTip = "Reply";
link.NavigateUrl = "ReplyForum.aspx?ParentId=" + dr["ForumItemId"].ToString() + "&MainForumId=" + dr["MainForumId"].ToString();
link.ImageUrl = "~/Images/btnPostReply.gif";
htmlForumItemCellButton.Attributes.Add("class", "tableItems");

//Create empty cell
HtmlTableCell htmlForumItemEmptyCell = new HtmlTableCell();
htmlForumItemEmptyCell.InnerHtml = "";
htmlForumItemEmptyCell.Attributes.Add("width", "100");
htmlForumItemEmptyCell.Attributes.Add("valign", "top");
htmlForumItemEmptyCell.Controls.Add(link);
htmlForumItemEmptyCell.Attributes.Add("class", "tableItems");
htmlForumItemRow.Cells.Add(htmlForumItemEmptyCell);

//Owner Name cell
HtmlTableCell htmlForumItemCellOwnerName = new HtmlTableCell();
htmlForumItemCellOwnerName.InnerHtml = dr["OwnerName"].ToString();
htmlForumItemCellOwnerName.Attributes.Add("class", "tableItems");
htmlForumItemRow.Cells.Add(htmlForumItemCellOwnerName);

//Cell date time
HtmlTableCell htmlForumItemCellDate = new HtmlTableCell();
htmlForumItemCellDate.InnerHtml = dr["CreateDate"].ToString();
htmlForumItemCellDate.Attributes.Add("class", "tableItems");
htmlForumItemRow.Cells.Add(htmlForumItemCellDate);

//Add rows to html table
htmlMainTable.Rows.Add(htmlForumItemRow);

if (x > 1)
{
//Create empty row
HtmlTableRow htmlEmptyRow = new HtmlTableRow();
HtmlTableCell htmlEmptyCell = new HtmlTableCell();
htmlMainTable.Rows.Add(htmlEmptyRow);

HtmlTableRow htmlEmptyRow2 = new HtmlTableRow();
HtmlTableCell htmlEmptyCell2 = new HtmlTableCell();
htmlMainTable.Rows.Add(htmlEmptyRow2);
}
x += 1;
}
}
}

No comments: