将 IQueryable 返回到数据绑定控件的正确语法是什么
我正在进行一些重构以消除 SqlDataSource 控件。下面是我的业务逻辑类中的代码,旨在返回给定 ID 的图像列表。
public class BusinessLogic
{
public IQueryable<SBMData2.File> GetPackageImages(int id)
{
SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();
IQueryable<SBMData2.File> PackageImages = from f in db.Files
where f.PackageID == id
select f;
return PackageImages;
}
}
在我后面的代码中,我尝试通过执行类似以下操作将 IQueryable 对象绑定到 asp 转发器。
int id = RadGrid1.SelectedValues["id"].ToString();
Repeater1.DataSource = BusinessLogic.GetPackageImages(id);
Repeater1.DataBind();
我的项目模板包含一个简单的图像标签,用于从绑定的 IQueryable 对象中绑定 FileName。例如:
<img src="PackageFile.ashx?Thumb=true&Id=<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() %>" alt="loading image ..."/>
我的问题有两个部分。首先,我在 BusinessLogic 类中的语法是否正确?我怀疑这不是因为我无法从后面的代码访问 GetPackageImages。其次,我几乎可以肯定我没有正确指定数据源。我该如何纠正这个问题?
谢谢你!
I'm doing some refactoring to eliminate SqlDataSource controls. Below is code from my business logic class that is intended to return a list of images for a given ID.
public class BusinessLogic
{
public IQueryable<SBMData2.File> GetPackageImages(int id)
{
SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();
IQueryable<SBMData2.File> PackageImages = from f in db.Files
where f.PackageID == id
select f;
return PackageImages;
}
}
In my code behind I'm trying to bind the IQueryable object to an asp repeater by doing something like the following.
int id = RadGrid1.SelectedValues["id"].ToString();
Repeater1.DataSource = BusinessLogic.GetPackageImages(id);
Repeater1.DataBind();
My item template contains a simple image tag that binds FileName from the bound IQueryable object. For example:
<img src="PackageFile.ashx?Thumb=true&Id=<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() %>" alt="loading image ..."/>
My question has two parts. First, is my syntax correct inside of the BusinessLogic class? I suspect it is not because I cannot access GetPackageImages from my code behind. Second, I am almost certain that I did not specify my datasource correctly. How can I correct that?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将其声明为实例方法,但您试图将其作为静态方法获取,除非您有一个名为
BusinessLogic的属性
类型BusinessLogic
... 尝试将其设为静态:我不太了解数据绑定,但看起来可能没问题。
就我个人而言,我不会在
GetPackageImages
中使用查询表达式,而且我也不会使用 PascalCase 命名局部变量,请注意。我只想用:You've declared it as an instance method, but you're trying to get at it as a static method, unless you've got a property called
BusinessLogic
of typeBusinessLogic
... Try just making it static:I don't know much about data binding, but it looks like it could be okay.
Personally I wouldn't use a query expression in
GetPackageImages
, and I also wouldn't name local variables with PascalCase, mind you. I'd just use: