LinqDataSource:过滤并绑定到gridview
问题没有按照我想要的方式解决,但我继续将功劳归于:ŁukaszW.pl,感谢他的时间和努力。
我正在使用 gridview 控件和 linqdatasource ,它一切正常,我已经添加了 searchBySubject 的功能,我添加了WhereParameters 并绑定了我的gridview(参见下面的代码),但不知何故它没有返回任何行,我看到我有基于我正在搜索的内容的行数。
protected void btnSearch_Click(object sender, EventArgs e)
{
this.LinqDataSource1.WhereParameters["Subject"].DefaultValue = this.txtSubject.Text;
this.GridView1.DataBind();
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="LinqDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
SortExpression="UserID" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyDataContextDataContext"
onselecting="LinqDataSource_Selecting" >
<WhereParameters>
<asp:Parameter Name="Subject" />
</WhereParameters>
</asp:LinqDataSource>
public List<Reporter> GetInquiries()
{
using (MyDataContextDataContext dc = conn.GetContext())
{
var loadAll = (from spName in dc.spReporter()
select spName);
List<Reporter> reporterList = new List<Reporter>();
foreach (var item in loadAll)
{
reporterList.Add(new Reporter(item.Id, item.InqDate, item.Subject));
}
return reporterList;
}
错误:
The query results cannot be enumerated more than once
the problem is not solved the way i wanted but i go ahead give the credit to : ŁukaszW.pl for his time and effort.
i am using gridview control and a linqdatasource and its all working fine and i have added the functionlity of searchingBySubject and i added WhereParameters and than binding my gridview (see the code below) but somehow its not returning any rows and i see i have number of rows based on what i am searching.
protected void btnSearch_Click(object sender, EventArgs e)
{
this.LinqDataSource1.WhereParameters["Subject"].DefaultValue = this.txtSubject.Text;
this.GridView1.DataBind();
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="LinqDataSource1"
EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
SortExpression="UserID" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyDataContextDataContext"
onselecting="LinqDataSource_Selecting" >
<WhereParameters>
<asp:Parameter Name="Subject" />
</WhereParameters>
</asp:LinqDataSource>
public List<Reporter> GetInquiries()
{
using (MyDataContextDataContext dc = conn.GetContext())
{
var loadAll = (from spName in dc.spReporter()
select spName);
List<Reporter> reporterList = new List<Reporter>();
foreach (var item in loadAll)
{
reporterList.Add(new Reporter(item.Id, item.InqDate, item.Subject));
}
return reporterList;
}
ERROR:
The query results cannot be enumerated more than once
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您声明一个 LinqDataSource ,例如在您的页面中:
您的 LinqDataSource。选择事件处理程序应该大致如下所示:
或者您可以将“主题”作为输入参数添加到存储过程和数据库中的过滤中。在这种情况下,事件处理程序将如下所示:
相关资源:
Assuming that you declare a LinqDataSource like this in your page:
Your LinqDataSource.Selecting event handler should roughly look like this:
Alternatively you could add 'Subject' as an input parameter to the Stored Procedure and to the filtering in the database. In that case the event handler would look like this:
Related resources:
关于这个问题,我没有看到您的 GetInquiries 方法和 LinqDataSource 之间有任何联系。这是第一,第二是即使有连接,如果您返回列表而不是 IQueriable 对象,它也无法工作...
为了更好地理解与 LinqDataSource 的绑定,请阅读 这篇 Scott Gu 的文章
另外,我想向您展示您的 GetInquiries 方法可以简化为以下形式:
---- 已编辑 ----
替代解决方案示例:
---------- 编辑 ------ - - - -
问候
About the problem, I don't see any connection between your GetInquiries method and LinqDataSource. Thats first, second is that even if there will be connection it will not work if you are returning list, and not IQueriable object...
To better uderstand binding with LinqDataSource read this Scott Gu's article
Also I want to show you that your GetInquiries method could be simplified to this form:
---- EDITED ----
Example of alternative solution:
---------- EDIT -------------
Regards