You must Sign In to post a response.
  • Category: ASP.NET

    How to create div dynamically using datbase

    I have one table as follows Latestnews

    id Subject Description Active
    1 Accounts page Accounts description page A
    2 Registration page Registration page A

    i want to create div dynamically from the latestnews table.

    i wan to create 2 div because in latestnews table 2 records are there

    for that how can i do in asp.net c#.
  • #764316
    Hi rao,
    Please refer following code snippet:

    //... Here dt= datatable with values retrieved from database and contains 3 columns as Subject, Description, Active
    HtmlGenericControl htmlDiv = null;
    HtmlTable tblMain = null;
    HtmlTableRow trRow = null;
    HtmlTableCell tCell1 = null;
    HtmlTableCell tCell2 = null;
    HtmlTableCell tCell3 = null;

    for (int iRowCount = 0; iRowCount < dt.Rows.Count; iRowCount++)
    {
    htmlDiv = new HtmlGenericControl("htmlDivId" + iRowCount);//... Create div with unique ID
    tblMain = new HtmlTable();//... Create table to show records from DB in table format
    tblMain.Width = "100%";

    if (iRowCount == 0)
    {
    #region ... Code for headers of table ...
    trRow = new HtmlTableRow();
    tCell1 = new HtmlTableCell();
    tCell2 = new HtmlTableCell();
    tCell3 = new HtmlTableCell();

    tCell1.Width = "33%";
    tCell2.Width = "33%";
    tCell3.Width = "33%";

    tCell1.InnerText = "Subject";
    tCell2.InnerText = "Description";
    tCell3.InnerText = "Active";

    trRow.Cells.Add(tCell1);
    trRow.Cells.Add(tCell2);
    trRow.Cells.Add(tCell3);
    tblMain.Rows.Add(trRow);
    #endregion
    }

    trRow = new HtmlTableRow();
    tCell1 = new HtmlTableCell();
    tCell2 = new HtmlTableCell();
    tCell3 = new HtmlTableCell();

    tCell1.Width = "33%";
    tCell2.Width = "33%";
    tCell3.Width = "33%";

    tCell1.InnerText = dt.Rows[iRowCount][0].ToString().Trim();
    tCell2.InnerText = dt.Rows[iRowCount][1].ToString().Trim();
    tCell3.InnerText = dt.Rows[iRowCount][2].ToString().Trim();

    trRow.Cells.Add(tCell1);
    trRow.Cells.Add(tCell2);
    trRow.Cells.Add(tCell3);
    tblMain.Rows.Add(trRow);

    htmlDiv.Controls.Add(tblMain); //... Add generated table to div
    this.Controls.Add(htmlDiv);//... Add div to webform
    }
    htmlDiv = null;
    tblMain = null;
    trRow = null;
    tCell1 = null;
    tCell2 = null;
    tCell3 = null;

    //... Output ....
    //Subject Description Active
    //sub1 desc1 act1
    //sub2 desc2 act2

    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments