How to get the values of DataTable in aspx page [Resolved]

Posted by Amatya under ASP.NET on 5/31/2016 | Points: 10 | Views : 409 | Status : [Member] | Replies : 3
How to get the values of DataTable in aspx page? I have the DataTable values in .cs page now i want to get that values in aspx page in script? How can I get that

Feel free to share informations 9731764134
Thanks



Responses

Posted by: A2H on: 5/31/2016 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
If you want to access the values in javascript then use a hidden field to pass the value like below .Here we are using Json , hence first we will serialize our datatable values to JSON using Json Serializer and then parse the Json back to array in javascript
C#:

//Loading dummy data
DataTable table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
table.Columns.Add("Column4", typeof(string));
// Here we add five DataRows.
table.Rows.Add("Sample1", "Sample2", "", "Sample4");
table.Rows.Add("TestValue1", "TestValue2", "TestValue3", "TestValue3");

//Creating Javascript seralizer object
JavaScriptSerializer objjavascriptSerializer = new JavaScriptSerializer();
//Convert the datatable to list
var dtlist = (from c in table.AsEnumerable()
select new
{
Column1 = c.Field<string>("Column1"),
Column2 = c.Field<string>("Column2"),
Column3 = c.Field<string>("Column3"),
Column4 = c.Field<string>("Column4")
}).ToList();
//Seralize the data and assign it to hidden control
HiddenField1.Value = objjavascriptSerializer.Serialize(dtlist);


Aspx Page:
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#<%=btnSecond.ClientID %>").click(function () {
//Parse the Json value
var dttable = JSON.parse($("#HiddenField1").val());
//Loop through each values in datatable
$(dttable).each(function (index, val) {

})
});
});
</script>


References:

https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

Thanks,
A2H
My Blog

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 5/31/2016 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You can use Gridview to display the data from datatable in aspx page.
Sample Code:
 <asp:GridView ID="GridView4" runat="server" AutoGenerateColums="false">
<Columns>
<%--Add your columns here--%>
</Columns>
</asp:GridView>


You can use Datasource to assign the datatable to Gridview
//Loading dummy data
DataTable table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
table.Columns.Add("Column4", typeof(string));
// Here we add five DataRows.
table.Rows.Add("Sample1", "Sample2", "", "Sample4");
table.Rows.Add("TestValue1", "TestValue2", "TestValue3", "TestValue3");
//Assigning datasoure to gridview
GridView4.DataSource=table;
GridView4.DataBind();


Also if you want to display a particular value then use a label control or textbox control
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
C#:
//Loading dummy data
DataTable table = new DataTable();
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(string));
table.Columns.Add("Column3", typeof(string));
table.Columns.Add("Column4", typeof(string));
// Here we add five DataRows.
table.Rows.Add("Sample1", "Sample2", "", "Sample4");
table.Rows.Add("TestValue1", "TestValue2", "TestValue3", "TestValue3");

Label2.Text=table.Rows[0]["Column1"].ToString();



Thanks,
A2H
My Blog

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muneshs16 on: 6/11/2016 [Member] Starter | Points: 25

Up
0
Down
go to following link

http://www.aspdotnet-suresh.com/2012/03/bind-data-and-display-datatable-on-aspx.html

Munesh

Amatya, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response