Click here to Skip to main content
15,906,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,


I have a HTML table called "Latest news" and i wanted to post the data from database table to the HTML table (ASPX) from codebehind(newsCafe.aspx.cs) on a page load event.

Any suggestion.

In PHP i had done this using AJAX. But in ASP.NET i want make this in CodeBehind.

Any suggestion...
Posted
Updated 30-May-14 19:10pm
v3

Why html table? use ASP.NET GridView or Repeater
check below link for sample code
How to populate DataGridView, GridView with SQL statement in C#[^]
note that ASP.NET GridView is rendered as a HTML Table when passed to the client (browser) and it is easy to use.
if you still need to go with html table check below links
http://stackoverflow.com/questions/19602917/how-to-display-data-in-html-table-in-asp-net[^]
http://stackoverflow.com/questions/8398604/how-do-i-bind-data-from-the-database-table-to-an-html-control-like-div-or-tab[^]
 
Share this answer
 
v2
try this example.. :)

Javascript

JavaScript
function BindTable() {

           $.ajax({
               type: "POST",
               url: "newsCafe.aspx/GetData",
               contentType: "application/json;charset=utf-8",
               data: {},
               dataType: "json",
               success: function (data) {

                   $("#grdDemo").empty();

                   if (data.d.length > 0) {
                       $("#grdDemo").append("<tr><th>Username</th>
                       <th>Firstname</th>  <th>Lastname</th>
                       <th>EmailID</th></tr>");
                       for (var i = 0; i < data.d.length; i++) {

                           $("#grdDemo").append("<tr><td>" +
                           data.d[i].Firstname + "</td> <td>" +
                           data.d[i].Lastname + "</td> <td>" +
                           data.d[i].Username + "</td> <td>" +
                           data.d[i].EmailID + "</td></tr>");
                       }
                   }
               },
               error: function (result) {
                   //alert("Error login");

               }
           });


HTML

HTML
<div id="grdDemo"> </div>


C#

C#
[WebMethod] 
        public static DetailsClass[] GetData() //GetData function
        {
            List<detailsclass> Detail = new List<detailsclass>();

            string SelectString = "Select Username,Firstname,Lastname,EmailID from DemoTable";
            SqlConnection cn = new SqlConnection("Data Source=servername;
            Initial Catalog=DemoDatabase;User ID=User;Password=*****");
            SqlCommand cmd = new SqlCommand(SelectString,cn);
            cn.Open();

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dtGetData = new DataTable();

            da.Fill(dtGetData);

            foreach(DataRow dtRow in dtGetData.Rows)
            {
                DetailsClass DataObj = new DetailsClass();
                DataObj.Username = dtRow["Username"].ToString();
                DataObj.Firstname = dtRow["Firstname"].ToString();
                DataObj.Lastname = dtRow["Lastname"].ToString();
                DataObj.EmailID = dtRow["EmailID"].ToString();

                Detail.Add(DataObj);            
            }

            return Detail.ToArray();
        }
        public class DetailsClass //Class for binding data
        {
            public string Username { get; set; }
            public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string EmailID { get; set; }
        
        }



COMPLETE REFERNCE.. :)
Bind Gridview using AJAX [^]
 
Share this answer
 
v2
Comments
PRAKASH_N 31-May-14 4:15am    
using web services... Thats the best idea... Thank u
DamithSL 31-May-14 4:24am    
OP clearly mentioned that need solution Without using AJAX

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