Use this method to convert a datatable to html string
public string GenerateHtmlFromDataTable(DataTable dt )
{
StringBuilder html = new StringBuilder();
html.Append("<table border="1">");
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<th>");
html.Append(column.ColumnName);
html.Append("</th>");
}
html.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append(row[column.ColumnName]);
html.Append("</td>");
}
html.Append("</tr>");
}
html.Append("</table>");
return html.ToString();
}
return the html string from your method like
public static string Getdata(string Id)
{
string SQL = "select * from testtable where UserId='" + Id + "'";
DataTable DATA = new DataTable();
SqlDataAdapter DTA = new SqlDataAdapter(SQL, cnnx);
DTA.Fill(DATA);
if (DATA.Rows.Count > 0)
{
return GenerateHtmlFromDataTable(DATA);
}
return "";
}
Use script like this
<script>
$(document).ready(function () {
$("#Btnclick").click(function () {
$.ajax({
type: "POST",
url: "WebForm1.aspx/Getdata",
data: "{'Id':'" + $("#textid").val() + "'}",
datatype: "JSON",
contentType: "application/json; charset=ut-8",
success:function(data)
{
alert(data.d);
$("#divtable").html(data.d)
},
error: function () {
alert("error");
}
});
});
});
</script>
Hope this help...