Create HTML DIV Dynamically

Introduction

This blog explains how we can generate a HTML Div dynamically. For this you must be aware of basic HTML, CSS.

Steps

  1. Create a static string variable html:
    1. public static string html = "";  
  2. Add <%=html%> in the aspx file inside of the from tag.

  3. Add a function like this:
    1. private string LoadDragEventsByUserID(string userId)  
    2. {  
    3.    try  
    4.    {   
    5.       DataSet ds = Fetch dataset from your server  
    6.       html = string.Empty;  
    7.       for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)  
    8.       {  
    9.          html += "<div id='" + ds.Tables[0].Rows[i]["Your Unique ID"].ToString() + "</div>";  
    10.       }  
    11.   
    12.       return html;  
    13.    }  
    14.    catch  
    15.    {  
    16.       return null;  
    17.    }  
    18. }  

Please note that your dataset rows will be contained different rows and columns, please change it according to your need. And also create css styles your own. If you need any help regarding style, feel free to ask me.