LinqDataSource - 过滤空值
我正在编写一个 ASP.Net Web 应用程序。我有 listview,它的数据源是 LinqDataSource。在我的数据库中,我有一个员工表,我正在尝试使用下拉列表过滤他们的团队的记录。这工作正常,直到我在下拉列表中选择“全部”。它返回除 teamID 为空的人员之外的所有人员。如何返回 teamID 为 null 的记录?
这是我的代码:
<asp:ListView ID="ListView1" runat="server" DataSourceID="ldsStaff" DataKeyNames="staffID">
<LayoutTemplate>
<table>
<tr>
<th>Name</th>
<th>Team</th>
</tr>
<tr>
<td> </td>
<td><asp:DropDownList ID="ddlTeamFilter" runat="server" DataSourceID="ldsTeams" DataTextField="Team" DataValueField="TeamID" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Text="[All]" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr ID="itemPlaceHolder" runat="server"></tr>
</table>
</LayoutTemplate>
....
</asp:Listview>
<asp:LinqDataSource ID="ldsStaff" runat="server"
ContextTypeName="ProjectDatabase.ProjectDatabaseUsersDataContext" OrderBy="name"
TableName="Staffs" EnableUpdate="True" Where="inService == @inService && TeamID == @TeamID">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="inService" Type="Boolean" />
<asp:ControlParameter ControlID="ctl00$ContentPlaceHolder1$ListView1$ddlTeamFilter" Name="TeamID" Type="Int32" PropertyName="SelectedValue"/>
</WhereParameters>
</asp:LinqDataSource>
在我的代码后面,我处理 LinqDataSource 选择事件:
Private Sub ldsStaff_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles ldsStaff.Selecting
Dim ddl As DropDownList = CType(ListView1.FindControlRecursive("ddlTeamFilter"), DropDownList)
If ddl.SelectedValue = "" Then
e.WhereParameters.Remove("TeamID")
End If
我认为如果我在下拉列表为“全部”时删除 where 参数,它将强制它返回所有记录,但它不起作用。
请帮忙!
谢谢, 艾玛
I am writing an ASP.Net web application. I have listview, it's datasource is a LinqDataSource. In my database, I have a staff table and I am trying filter for records by their team using a dropdownlist. This works fine, until I select "All" in the dropdownlist. It returns all staff except for the ones where the teamID is null. How can I return the records where teamID is null?
This is my code:
<asp:ListView ID="ListView1" runat="server" DataSourceID="ldsStaff" DataKeyNames="staffID">
<LayoutTemplate>
<table>
<tr>
<th>Name</th>
<th>Team</th>
</tr>
<tr>
<td> </td>
<td><asp:DropDownList ID="ddlTeamFilter" runat="server" DataSourceID="ldsTeams" DataTextField="Team" DataValueField="TeamID" AppendDataBoundItems="true" AutoPostBack="true">
<asp:ListItem Text="[All]" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr ID="itemPlaceHolder" runat="server"></tr>
</table>
</LayoutTemplate>
....
</asp:Listview>
<asp:LinqDataSource ID="ldsStaff" runat="server"
ContextTypeName="ProjectDatabase.ProjectDatabaseUsersDataContext" OrderBy="name"
TableName="Staffs" EnableUpdate="True" Where="inService == @inService && TeamID == @TeamID">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="inService" Type="Boolean" />
<asp:ControlParameter ControlID="ctl00$ContentPlaceHolder1$ListView1$ddlTeamFilter" Name="TeamID" Type="Int32" PropertyName="SelectedValue"/>
</WhereParameters>
</asp:LinqDataSource>
In my code behind I handle the LinqDataSource selecting event:
Private Sub ldsStaff_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles ldsStaff.Selecting
Dim ddl As DropDownList = CType(ListView1.FindControlRecursive("ddlTeamFilter"), DropDownList)
If ddl.SelectedValue = "" Then
e.WhereParameters.Remove("TeamID")
End If
I thought that if I removed the whereparameter when the dropdownlist is "All" it would force it to return all the records, but it doesn't work.
Please help!
Thanks,
Emma
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您无论如何都要处理选择事件,我会一起删除数据源中的Where子句,并处理选择事件中的所有内容,将e.Result值设置为您的linq查询。
然后在您的 ddlTeamFilter 中重新绑定发生更改的列表视图。
希望这有帮助。
If you are handling the selecting event anyway I would remove the Where clause in your datasource all together and handle everything in the selecting event setting the e.Result value to your linq query.
Then in your
ddlTeamFilter
rebind the listview on change.Hope this helps.