如何将代码中的ADO DataTable(在页面加载时可用)返回到javascript?

时间:2022-02-14 18:22:39

On page load, I have my DataTable results available which I need to pass back to the javascript for processing.

在页面加载时,我有可用的DataTable结果,我需要将其传递回javascript进行处理。

What are my options?

我有什么选择?

  1. Use a hidden field to pass the data back up. not sure how exactly, by maybe convert it to xml/json and then access it from javascript that way. Seems like a pain. No extra round trips for this approach.
  2. 使用隐藏字段将数据传回。不知道究竟是多么准确,可能将其转换为xml / json,然后以这种方式从javascript访问它。好像很痛苦。没有额外的往返这种方法。

  3. Use webmethod/webservice to issue a call directly from the javascipt and then get back the DataTable, however this requires an extra round trip since I already have the DataTable available on pageload.
  4. 使用webmethod / webservice直接从javascipt发出调用,然后返回DataTable,但这需要额外的往返,因为我已经在pageload上有了DataTable。

  5. It is also possible to access objects in code behind using ASP.NET inline expressions (i.e. <% syntax) usable from the .aspx page. No extra round trips for this approach.
  6. 也可以使用.aspx页面中可用的ASP.NET内联表达式(即<%语法)访问后面的代码中的对象。没有额外的往返这种方法。

  7. Convert DataTable to json/xml and then use ASP's ClientScript.RegisterStartupScript to make it available in there as a string returned from a function or something. Sounds hacky though.
  8. 将DataTable转换为json / xml,然后使用ASP的ClientScript.RegisterStartupScript将其作为从函数或其他内容返回的字符串提供。听起来很丑陋。

  9. Bind the DataTable/DataSet (or any object that implements the IEnumerable interface) to an ASP data control such as a DataGrid, DataList, Repeater, etc... and then just hide the control via some CSS: #datacontrol {display: none;}
  10. 将DataTable / DataSet(或实现IEnumerable接口的任何对象)绑定到ASP数据控件(如DataGrid,DataList,Repeater等)......然后通过一些CSS隐藏控件:#datacontrol {display:none; }

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


0  

If it is just array, refer this.

如果它只是数组,请参考。

If it is custom object, IMO it is better to call server method via. ajax requests and load them into javascript objects and work on data.

如果是自定义对象,IMO最好通过调用服务器方法。 ajax请求并将它们加载到javascript对象中并处理数据。

#2


0  

There are a number of ways you could do it as you have already reasoned through. Any time I have had a need to do this I have written my data out to a javascript variable using the clientscript.registerclientscriptblock method and then used javascript in the page to access the variable and do the rest of the parsing. See this for example

您可以通过多种方式完成这项工作。每当我需要这样做时,我都会使用clientscript.registerclientscriptblock方法将数据写入javascript变量,然后在页面中使用javascript来访问变量并执行其余的解析。例如,见这个

You would just use the string builder or whatever you chose to dim a javascript array containing your table values and pass that to the page.

您只需使用字符串构建器或您选择的任何内容来调暗包含表值的javascript数组,并将其传递给页面。

#3


0  

I had this same requirement in one of my asp.net application. What I did was, I created a string variable from the DataTable on Page_Load() by using custom separators like :: ; etc for row-separator and column separator. And then set the string to a hidden field or textbox text with CSS {display: none;}

我在我的一个asp.net应用程序中有同样的要求。我做的是,我通过使用自定义分隔符(例如::)从Page_Load()上的DataTable创建了一个字符串变量。行分隔符和列分隔符等。然后使用CSS {display:none;}将字符串设置为隐藏字段或文本框文本

After that, you can get the value of the hidden-field or TextBox in javascript $(document).ready() block or javascript pageLoad function() if you want it to be executed on every full/partial postback.

之后,如果您希望在每个完整/部分回发上执行它,您可以在javascript $(document).ready()块或javascript pageLoad函数()中获取hidden-field或TextBox的值。

Get the value and decode it as you had coded the DataTable into the string in code-behind, and process it as u need.

获取值并解码它,因为您已将DataTable编码为代码隐藏中的字符串,并根据需要进行处理。

Hope it helps :)

希望能帮助到你 :)

#4


0  

After trying several of the above options, I've found #3 (ASP.NET inline expressions) to be the best choice for accessing an ADO query result such as a DataTable or DataSet since it was the quickest to implement and had no additonal round trips due to the fact the inline expressions are resolved during the page construction.

在尝试了上述几个选项之后,我发现#3(ASP.NET内联表达式)是访问ADO查询结果(如DataTable或DataSet)的最佳选择,因为它是最快的实现并且没有附加的圆形由于在页面构造期间内联表达式已解决的事实,因此会跳闸。

I tried #5 (bind data to data control, then hide it), but it was noticeably slower and I had problems finding a control that would expose all the features of a DataTable/DataSet. There are a handful of data controls that you can bind records to, but what I found was that much of the "nice-ities" that come with a DataSet/DataTable are lost when converting to a repeater control or such. If you use one of the more full featured ASP controls available to get more of those features back, you lose on performance since those controls are meant for read/write and to render the content for display. And it wasn't as simple as I expected to access the data as it is in the code behind.

I thought about #4 (passing data thru ASP's ClientScript.RegisterStartupScript), but didn't feel like serializing/deserializing every object I need to expose and working though any hiccups that come with it. It just didn't seem like the right way. I suppose it would be fine for simpler objects though.

And #1 (serialize data to a hidden field) is pretty much the same concept as the above #4 (passing data thru ASP's ClientScript.RegisterStartupScript so I didn't bother with that one either.

The other (2nd best) possibility is to do #2 (using webmethod/webservice) as @Sundeep and @ron_tornambe have pointed out. However, this option adds an extra round trip to the page request and since the above scenario has the DataTable/DataSet ready for consumption on page load it is not optimal for me. If it wasn't for that I'd say it was equal to my first choice of #3 (ASP.NET inline expressions) since you'll get the full featured object to work with.

我尝试了#5(将数据绑定到数据控件,然后将其隐藏),但它明显变慢,我在查找将暴露DataTable / DataSet的所有功能的控件时遇到问题。有一些数据控件可以绑定记录,但我发现,当转换为转发器控件等时,DataSet / DataTable附带的大部分“好处”都会丢失。如果您使用一个功能更全面的ASP控件来获取更多这些功能,则会导致性能下降,因为这些控件用于读/写并呈现内容以供显示。它并不像我预期的那样简单,因为它在后面的代码中是访问数据。我想到了#4(通过ASP的ClientScript.RegisterStartupScript传递数据),但是感觉不像序列化/反序列化我需要公开的每个对象,并且工作时会遇到它带来的任何打嗝。它似乎不是正确的方式。我想对于更简单的对象会更好。而#1(将数据序列化为隐藏字段)与上述#4几乎相同(通过ASP的ClientScript.RegisterStartupScript传递数据,所以我也没有打扰那个。另一个(第二好的)可能性是做#2(使用webmethod / webservice)作为@Sundeep和@ron_tornambe指出。但是,这个选项为页面请求增加了额外的往返,因为上面的场景已经准备好在页面加载时使用DataTable / DataSet了对我来说并不是最优的。如果不是这样的话,我会说它等于我第一次选择的#3(ASP.NET内联表达式),因为你将获得使用的全功能对象。

#1


0  

If it is just array, refer this.

如果它只是数组,请参考。

If it is custom object, IMO it is better to call server method via. ajax requests and load them into javascript objects and work on data.

如果是自定义对象,IMO最好通过调用服务器方法。 ajax请求并将它们加载到javascript对象中并处理数据。

#2


0  

There are a number of ways you could do it as you have already reasoned through. Any time I have had a need to do this I have written my data out to a javascript variable using the clientscript.registerclientscriptblock method and then used javascript in the page to access the variable and do the rest of the parsing. See this for example

您可以通过多种方式完成这项工作。每当我需要这样做时,我都会使用clientscript.registerclientscriptblock方法将数据写入javascript变量,然后在页面中使用javascript来访问变量并执行其余的解析。例如,见这个

You would just use the string builder or whatever you chose to dim a javascript array containing your table values and pass that to the page.

您只需使用字符串构建器或您选择的任何内容来调暗包含表值的javascript数组,并将其传递给页面。

#3


0  

I had this same requirement in one of my asp.net application. What I did was, I created a string variable from the DataTable on Page_Load() by using custom separators like :: ; etc for row-separator and column separator. And then set the string to a hidden field or textbox text with CSS {display: none;}

我在我的一个asp.net应用程序中有同样的要求。我做的是,我通过使用自定义分隔符(例如::)从Page_Load()上的DataTable创建了一个字符串变量。行分隔符和列分隔符等。然后使用CSS {display:none;}将字符串设置为隐藏字段或文本框文本

After that, you can get the value of the hidden-field or TextBox in javascript $(document).ready() block or javascript pageLoad function() if you want it to be executed on every full/partial postback.

之后,如果您希望在每个完整/部分回发上执行它,您可以在javascript $(document).ready()块或javascript pageLoad函数()中获取hidden-field或TextBox的值。

Get the value and decode it as you had coded the DataTable into the string in code-behind, and process it as u need.

获取值并解码它,因为您已将DataTable编码为代码隐藏中的字符串,并根据需要进行处理。

Hope it helps :)

希望能帮助到你 :)

#4


0  

After trying several of the above options, I've found #3 (ASP.NET inline expressions) to be the best choice for accessing an ADO query result such as a DataTable or DataSet since it was the quickest to implement and had no additonal round trips due to the fact the inline expressions are resolved during the page construction.

在尝试了上述几个选项之后,我发现#3(ASP.NET内联表达式)是访问ADO查询结果(如DataTable或DataSet)的最佳选择,因为它是最快的实现并且没有附加的圆形由于在页面构造期间内联表达式已解决的事实,因此会跳闸。

I tried #5 (bind data to data control, then hide it), but it was noticeably slower and I had problems finding a control that would expose all the features of a DataTable/DataSet. There are a handful of data controls that you can bind records to, but what I found was that much of the "nice-ities" that come with a DataSet/DataTable are lost when converting to a repeater control or such. If you use one of the more full featured ASP controls available to get more of those features back, you lose on performance since those controls are meant for read/write and to render the content for display. And it wasn't as simple as I expected to access the data as it is in the code behind.

I thought about #4 (passing data thru ASP's ClientScript.RegisterStartupScript), but didn't feel like serializing/deserializing every object I need to expose and working though any hiccups that come with it. It just didn't seem like the right way. I suppose it would be fine for simpler objects though.

And #1 (serialize data to a hidden field) is pretty much the same concept as the above #4 (passing data thru ASP's ClientScript.RegisterStartupScript so I didn't bother with that one either.

The other (2nd best) possibility is to do #2 (using webmethod/webservice) as @Sundeep and @ron_tornambe have pointed out. However, this option adds an extra round trip to the page request and since the above scenario has the DataTable/DataSet ready for consumption on page load it is not optimal for me. If it wasn't for that I'd say it was equal to my first choice of #3 (ASP.NET inline expressions) since you'll get the full featured object to work with.

我尝试了#5(将数据绑定到数据控件,然后将其隐藏),但它明显变慢,我在查找将暴露DataTable / DataSet的所有功能的控件时遇到问题。有一些数据控件可以绑定记录,但我发现,当转换为转发器控件等时,DataSet / DataTable附带的大部分“好处”都会丢失。如果您使用一个功能更全面的ASP控件来获取更多这些功能,则会导致性能下降,因为这些控件用于读/写并呈现内容以供显示。它并不像我预期的那样简单,因为它在后面的代码中是访问数据。我想到了#4(通过ASP的ClientScript.RegisterStartupScript传递数据),但是感觉不像序列化/反序列化我需要公开的每个对象,并且工作时会遇到它带来的任何打嗝。它似乎不是正确的方式。我想对于更简单的对象会更好。而#1(将数据序列化为隐藏字段)与上述#4几乎相同(通过ASP的ClientScript.RegisterStartupScript传递数据,所以我也没有打扰那个。另一个(第二好的)可能性是做#2(使用webmethod / webservice)作为@Sundeep和@ron_tornambe指出。但是,这个选项为页面请求增加了额外的往返,因为上面的场景已经准备好在页面加载时使用DataTable / DataSet了对我来说并不是最优的。如果不是这样的话,我会说它等于我第一次选择的#3(ASP.NET内联表达式),因为你将获得使用的全功能对象。