Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a queries :-

I have a database table which has rows.
Based on table rows i want to create div tag dynamically.

My requirement is like I have to create div based on database rows count.
if 5 rows are there on db, then 1 div will be created.
if 20 rows are there on db, then 2 div will be created.
if 25 rows are there on db, then 3 div will be created.
if 56 rows are there on db, then 6 div will be created.


1st div beside 2nd div.

Below are the javascript function:-


JavaScript
function LoadData(data)
{   
    var rows_count = 22; -- will be fetched from DB side
    var rowNum = Math.ceil(parseFloat(rows_count));

    var resultHtml = '';

    resultHtml += "<table style = \'width:100%;\' border=\'0\' colspan=\'2\' id=\'tbl_user\'>";    
    for (var i = 0; i <=rowNum-1 ; i++) {
        resultHtml += '<tr>';
        resultHtml += '<td border = 1><input type="name" placeholder="text goes here..."></td>';        
        resultHtml += '</tr>';         
        }
    }  
    resultHtml += '</table>';
}


Please help me out.
Many thanks !!!!!

What I have tried:

I have written logic but 22 rows are creating on one div tag.
I want to show 10 records in 1 div and another 10 records in 2nd div and so on..
Please help me out.
Posted
Updated 5-Apr-18 8:33am
v2
Comments
Ram Nunna 10-Jan-18 1:17am    
where did you write "Div" code? I didn't see any code for div.
Can you please update your question with "Div" logic.

Try This

function LoadData(data)
{   
    var rows_count = 10; -- will be fetched from DB side
    var rowNum = Math.ceil(parseFloat(rows_count));

    var resultHtml = '';

    resultHtml += "<table style = \'width:100%;\' border=\'0\' colspan=\'2\' id=\'tbl_user\'>";    
    for (var i = 1; i <=rowNum ; i++) {
        resultHtml += '<tr>';
        resultHtml += '<td border = 1><input type="name" placeholder="text goes here..."></td>';        
        resultHtml += '</tr>';         
        }
    }  
    resultHtml += '</table>';
}
 
Share this answer
 
If you have a specific row count then you may try something like this...

C#
for (int i = 0; i < Dt.Rows.Count; i++)
{
    if (i == 5)
    {
        HtmlGenericControl div1 = new HtmlGenericControl("div");                        
        div1.ID = div1 + i.ToString();
        div1.InnerText = "First div created for 5 rows";
        form1.Controls.Add(div1);
    }

    else if (i == 20)
    {
        HtmlGenericControl div2 = new HtmlGenericControl("div");
        div2.ID = div2 + i.ToString();
        div2.InnerText = "Second div created for 20 rows";
        form1.Controls.Add(div2);
    }

     //and so on
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900