ラベル datatable の投稿を表示しています。 すべての投稿を表示
ラベル datatable の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

returing a datatable

I'm having a problem trying to get a datatable from a CodeBehind using Pagemethods with ASP.NET Ajax Rc that was just released. The specific error is below. This code worked with the July CTP. If you have any suggestions, let me know.

System.InvalidOperationException A Circular reference was detedted while serializing an object of type System.Reflection.Module

Returning a datatable - solution

1) add reference ASP.NET 2.0 AJAX Futures December CTP DLL to solution,

2) add the following section to web. config

<

scripting>

<

webServices>

<

jsonSerialization>

<

converters>

<

addname="DataSetConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>

<

addname="DataRowConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>

<

addname="DataTableConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>

</

converters>

</

jsonSerialization>

</

webServices>

</scripting>

3) Add ASP.NET 2.0 AJAX Futures December CTP 'PreviewScript.js' to ur solution

4) Add the Script reference ur aspx page

<asp:ScriptManagerID="ScriptManager1"runat="server"><Scripts><asp:ScriptReferencePath="PreviewScript.js"/></Scripts></asp:ScriptManager>

5) we need to change the PageMethod(in code behind page that returns DataTable) toStatic

6)in order to get the result this way( result.getItem(i).getProperty("Column name");) we need to parse

Sys.Preview.Data.DataTable.parseFromJson(result);


This doesn't work anymore in v1.0. Anyone know how to fix it?

jsonSerialization>

<

converters>

<

addname="DataSetConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>

<

addname="DataRowConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>

<

addname="DataTableConverter"type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>

</

converters>

</

jsonSerialization>


This does work, I just forgot to download the January Futures CTP.

return a html table with ajax C#

hi

I am writing a website using C#, web services and ajax. I have my ajax web service call returning either an array or a DataTable object. Does anyone have any example of writing either one of these returned values to a html table using javascript?

thanks

C

It might help http://dotnetslackers.com/articles/ajax/ASPNETAjaxGridAndPager.aspx

Return a DataTable with Webservice and Bind via JavaScript

I am using a Webserivce method that returns a DataTable and in JavaScript I have this:

function pageLoad() {
ret = Dashboard.FetchQuadOne(OnCompleteOne, OnTimeOut, OnError);
ret = Dashboard.FetchQuadTwo(OnCompleteTwo, OnTimeOut, OnError);
}

function OnCompleteOne(result) {
document.getElementById('div1').innerHTML = result;
}

If I have my Webservice method, FetchQuadOne return a DataTable, how can I bind that to a GridView using JavaScript? Or is there a way to do this in the code-behind of my .aspx page? I guess I could just build an html table in my webservice method and return that, but it would be mugh each easier to bind the DataTable to a GridView.

I don't see how this could work, a DataTable is a .NET specific object. Javascript has no knowledge of how to consume that or for that matter any knowledge of how to bind to a .NET Datagrid. You could return xml or JSON and bind to some other data structure using XSL or roll your own.

Yeah, that is what I was afraid of. I thought maybe there would be someone way to put the JavaScript in the code-behind page and somehow bind the DataTable to a GirdView. I wanted all the built-in features of the GridView, like sorting and formating. O- well. I just wanted to make sure it wasn't possible before I head down an ugly road of building the table on the fly with the data.

Thanks.


Hi,

I think this article ofKazi Manzur Rashid can help you:http://dotnetslackers.com/articles/ajax/ASPNETAjaxGridAndPager.aspx

This article will show you how to create an AJAX Grid and a generic pager, which mimics the built-in GridView control on the client side.

Best Regards,

2012年3月24日土曜日

Returning a datatable

Based on trying to return a DataSet, I have decided to try and justreturn a DataTable (fyi. http://forums.asp.net/1077805/ShowPost.aspx).
The problem is that in my javascript callback, I need to get at thedata that is returned. What methods are available on the returneddata? I have a datatable with several datarows, but I don't knowwhat methods are available to actually get at the data. BTW, I amtrying to do this programmatically and am not interested in adeclarative solution to this. When I attempt to alert out to theUI, result.length is coming through a undefined.
Web Service:
[WebMethod]
public System.Data.DataTable ReturnDataSet()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("tblStateId",System.Type.GetType("System.Int32")));
dt.Columns.Add(new DataColumn("State",System.Type.GetType("System.String")));
dr = dt.NewRow();
dr["tblStateId"] = 1;
dr["State"] = "Tennessee";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["tblStateId"] = 2;
dr["State"] = "Alabama";
dt.Rows.Add(dr);
return (dt);
}
Client Side Javascript:
function LoadTest(){
Samples.AspNet.WebServiceTest.ReturnDataSet(ReturnDataTableCallBack);
}

function ReturnDataTableCallBack(result)
{
var i = 0;
var ddl = document.getElementById("sState");
var optionItem;
iLength = document.getElementById("sState").options.length;
document.getElementById("sState").visible = true;
document.getElementById("txtAreaResult").value = result.get_data();
for(i=0; i<iLength; i++)
{
document.getElementById("sState").options[0] = null;
}

for(i=0; i<result.length; i++)
{
document.getElementById("sState").options.add(newOption(resultIdea [I]["State"],resultIdea [I]["tblStateId"]))
}
}


I think you should be able to write code like:
for (var i = 0; i < results.get_length(); i++) {
alert(results.getItem(i).State);
}
As a side note: there are several ways that you can use to 'discover'the structure of an object. One way is to usedebug.dump(object,name[,recursive[,indentationPadding]]), which shouldadd information about an object to the trace. Another way is to loopthrough the members of an object, like "for (m in myObject) alert(m);".

Great information. It got me on the right track. I foundthat the following pseudo-javascript was what was necessary:
for(i=0; i<result.get_length(); i++)
{
var optAdd = newOption(result.getItem(i).getProperty("State"),result.getItem(i).getProperty("tblStateId"));
document.getElementById("sState").options.add(optAdd);
}
The .State and .tblStateId properties did not return a value, but thegetProperty("State") and getProperty("tblStateId") did what wasnecessary.
Do you have a complete example of the debug.dump() method?
Wally

You could use "get_State()" instead of "getProperty('State')" too (which I think is nicer).
To use debug.dump, try something like "debug.dump(results, "my results", true)".

How would you do this declaratively? For example, if I wanted to bind the first state returned to a label...I've tried this, and it doesn't work:
<binding id="binding1" dataContext="dataSource1" dataPath="data[0].State" property="text" automatic="false" />
I can't figure out what to put in the dataPath. Anyone have some guidance?