如何根据一个ObjectDataSource设置另一个ObjectDataSource的返回值
我有两个 Asp.net ListView 控件绑定到两个不同的 ObjectDataScource 控件。 每个 ODS 控件引用“MethodA”和“MethodB”。
我希望“MethodA”调用数据库并返回“MethodA”和“MethodB”的数据。
我总是可以让“MethodB”对数据库进行第二次调用,但这效率不高。
我不确定完成此任务的最佳方法。
[DataObjectMethod(DataObjectMethodType.Select)]
public List<int> MethodA(int input)
{
List<int> a = new List<int>();
List<string> b = new List<string>();
///
/// Make one call to database
/// returns: List<int> and List<string>
/// set 'a' and 'b' values.
return a;
}
[DataObjectMethod(DataObjectMethodType.Select)]
public List<string> MethodB()
{
List<string> b = new List<string>();
///
/// When MethodA is called set 'b'
///
return b;
}
I have two Asp.net ListView Controls bound to two different ObjectDataScource Controls. Each ODS control references 'MethodA' and 'MethodB'.
I want 'MethodA' to make one call the the database and return data for both 'MethodA' and 'MethodB'.
I could always have 'MethodB' make a second call to the database, but that would not be efficient.
I am not sure the best way to accomplish this.
[DataObjectMethod(DataObjectMethodType.Select)]
public List<int> MethodA(int input)
{
List<int> a = new List<int>();
List<string> b = new List<string>();
///
/// Make one call to database
/// returns: List<int> and List<string>
/// set 'a' and 'b' values.
return a;
}
[DataObjectMethod(DataObjectMethodType.Select)]
public List<string> MethodB()
{
List<string> b = new List<string>();
///
/// When MethodA is called set 'b'
///
return b;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不容易实现。
您可以删除 ObjectDatasource 并自行滚动它或绕过它。
也许在该类中使用 [ThreadLocal] 静态变量并让 MethodA 将值放入该变量中? MethodB 可以读取它。
并
从页面开头调用 Reset(),以便 ASP.NET 回收的每个线程都不会为下一个请求留下旧的过时数据。 我有没有提到这是一个黑客行为?
更好的解决方案:
MethodB 似乎不带参数。 因此,无论 MethodB 查询什么,都让 MethodA 获取它并将其推送到 HttpCache 中。
我所做的是,我的后端向网络服务器返回一个相当大(10 个表)的完整数据集,其中包含所有静态数据。 在那里我有一堂课,其中也有像你的 MehtodA 和 MehtodB 这样的东西。 但他们总是获取数据集。 GetDataSet() 查询 cahce,如果丢失,则查询 Web 服务并将其放入 cahce 中。 我的每个 MethodA 方法都只是使用 LINQ 从大数据集中获取内容。
当然,这只适用于静态数据......
I don't think that this is easily possible.
You could drop ObjectDatasource and roll it on your own or hack around it.
Maybe use a [ThreadLocal] static variable in that class and let MethodA put the value into that variable? MethodB could read it.
And a
Call Reset() from the start of your page so that each thread that is recycled by ASP.NET will not leave old stale data for the next request. Did I mention that this is a hack?
Better solution:
It seems like that MethodB does not take a parameter. So whatever it is that MethodB queries, let MethodA fetch it and push it into a HttpCache.
What I did was that my backend returend a quite big (10 tables) complete dataset to the webservers with all static data. There I had a class that had also things like your MehtodA and MehtodB. But they always fetched the dataset. GetDataSet() queried the cahce and if it was missing queried the webservice and put it in the cahce. Every of my MethodA methods simply used LINQ to get stuff from the big dataset.
Of course that only works with static data...