Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello
in HTML file, I have a list that is being repeated to create the list as shown below:
HTML
<div class="item">
  <div class="gallery-carousel-item">                              
    
      
        <div class="hover-mask-container">
           <div class="hover-zoom">
            <a href="project-single.html">Product A
              <span class="icon-container">
                
              </span>
            </a>
           </div>
        </div> 
    
  </div>
</div>
<!-- The code will be repeated here -->
<div class="item">
  <div class="gallery-carousel-item">                              
    
      
        <div class="hover-mask-container">
           <div class="hover-zoom">
            <a href="project-single.html">Product B
              <span class="icon-container">
                
              </span>
            </a>
           </div>
        </div>
    
  </div>
</div>


How can I create this list dynamically based on my records in sql data base using JavaScript ?
Thanks in Advance

What I have tried:

I have tried jqxWidjets but it will not make the UI that i want. I have just can make it using asp:ListView control but I want to make it using HTML tags.
for example:
HTML
Try it


function myFunction() {
    var x = document.createElement("UL");
    x.setAttribute("id", "myUL");
    document.body.appendChild(x);

    var a = ["Alpha","Beta","Gama"];
    for(i=0 ; i<=2 ; i++)
    {
       var y = document.createElement("LI");
       var t = document.createTextNode(a[i]);
       y.appendChild(t);
       document.getElementById("myUL").appendChild(y);
    }
}


Produces:
Alpha
Beta
Gama

based on "a" variables data(JSON data), How can I make the mentioned List based on SQL server data base records
Posted
Updated 22-Nov-16 7:28am
v2
Comments
Cristina Carrasco Angulo 18-Nov-16 17:43pm    
Can you use jquery?, why vanilla javascript?
Ali Majed HA 18-Nov-16 23:27pm    
Hi Cristina, Yes I can use JQuery.

Here's an example: c# asp.net
Jquery Examples

How it works:
Get List

Add this:
Json.NET

How to add...
Add Newton json


.js
JavaScript
function GetList() {
    $.ajax({
        async: false,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "ExampleList.aspx/GetList",
        dataType: "json",
        success: function (data, textStatus) {
            if (textStatus == "success") {
                var datos = jQuery.parseJSON(data.d);

                $("#List").html('');
                var list = '<ul>'
                $.each(datos, function (i, v) {
                    //list += '<li id="'+v.id+'">' + v.id + "-" + v.value + '</li>'
                    list += '<li id="'+v.id+'">' + v.value + '</li>'
                }); 
                list += '</ul>'

                $("#List").append(list);
            }
        },
        error: function (request, status, error) {
            alert(jQuery.parseJSON(request.responseText).Message);
        }
    });
}


HTML|ASP
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExampleList.aspx.cs" Inherits="JqueryExamples.ExampleList" %>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <input type="button" value="Get List" onclick="GetList()"/>
    <div id="List">
        
    </div>
    </form>
    <script src="Scripts/jquery.js"></script>
<script src="Scripts/ExampleListsa.js"></script>
</body>
</html>



Code Behind:
C#
        [WebMethod]
        public static string GetList()
        {
            List<data> data = new List<data>();
            try
            {
                data.Add(new Data { id = 1, value = "Alpha" });
                data.Add(new Data { id = 2, value = "Beta" });
                data.Add(new Data { id = 3, value = "Gamma" });
                data.Add(new Data { id = 4, value = "Delta" });
                data.Add(new Data { id = 5, value = "Epsilon" });
                data.Add(new Data { id = 6, value = "Zeta" });

                var rnd = new Random();
                data = data.OrderBy(item => rnd.Next()).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            return JsonConvert.SerializeObject(data);
        }


        public partial class Data {
            public int id { get; set; }
            public string value { get; set; }
        }

</data></data>



Can add a Class with a method to get the data from SQL, here's and example how to connect to SQL:
How to Connect SQL Database

In the Code Behind change the GetList Method for this:
C#
public static string GetList()
       {
           List<data> data = new List<data>();
           try
           {
               data = SqlClass.GetList();//Need to created the SqlClass
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message, ex);
           }
           return JsonConvert.SerializeObject(data);
       }
 
Share this answer
 
v3
Comments
Cristina Carrasco Angulo 22-Nov-16 13:35pm    
You can change:
List data = new List();

for some like this:
List data = SqlClass.GetData();

You need to create the SqlClass add the GetData method...
Ali Majed HA 22-Nov-16 13:58pm    
Hello Cristina
Thanks in Advance for your solution
Cristina Carrasco Angulo 22-Nov-16 18:37pm    
You're welcome
you can use jquery template for a neat soln to avoid manually creating dom structure.

Meanwhile more sophisticated soln is to use aspnet Repeater control to use plain html as u want.

Both above soln. are very easy in ASP.NET

for jquery template

Basics of jQuery Templates[^]

you could use Register array from server side if you don't want to use ajax call.

for repeater

How to: Add a Repeater Control to a Web Forms Page[^]
 
Share this answer
 

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