我在代码隐藏中使用ASP.NET(4.0 Framework)和VB.NET。我有这个SqlDataSource,我的gridview用作数据源:
<asp:SqlDataSource ID="repairApproverData" runat="server"
ConnectionString="<%$ ConnectionStrings:SystemsConnectionString %>"
SelectCommand="SELECT CASE(dateFixed) WHEN ISNULL(dateFixed,1) THEN DATEDIFF(day,[date],[dateFixed]) ELSE DATEDIFF(day,[date],GETDATE()) END AS daysWaited,[id],[date],[lot],[stock],[vin],[makeModel],[issue],[approved],[dateFixed],[comments] FROM [repairApprover] WHERE [lot] = IsNull(@lot,lot)"
DeleteCommand="DELETE FROM [repairApprover] WHERE id = @id"
CancelSelectOnNullParameter="false"
>
正如您在SelectCommand结束时所看到的,我有WHERE [lot] = IsNull(@lot,lot)
引用此ControlParameter:
<SelectParameters>
<asp:ControlParameter ControlID="lotBox" Name="lot" PropertyName="Text" Type="String" />
</SelectParameters>
控制参数从DropDownList获取过滤批次的值:
<asp:DropDownList ID="lotBox" runat="server" ClientIDMode="Static" AutoPostBack="True">
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="40">40</asp:ListItem>
</asp:DropDownList>
这一切都很棒。但是,我不知道如何添加另一个DropDownList,允许用户在名为“dateFixed”的列中过滤掉/具有NULL值的行。
基本上我需要能够做一些与上面类似的事情来确定以下三个字符串中的哪一个被添加到我的SelectCommand
AND [dateFixed] IS NOT NULL
AND [dateFixed] IS NULL
,第三个字符串为空。
答案 0 :(得分:0)
您需要动态构建SELECT
。像这样:
var sel = "Select ......FROM ...... ";
bool hasWhere;
if (combo1.SelectedIndex > -1)
{
if (!hasWhere)
{
hasWhere = true;
sel += " where ";
}
sel += "<your where condition 1>";
}
if (combo2.SelectedIndex > -1)
{
if (!hasWhere)
{
hasWhere = true;
sel += " where ";
}
else
sel += " and ";
sel += "<your where condition 1>";
}
if (combo3.SelectedIndex > -1)
{
if (!hasWhere)
{
hasWhere = true;
sel += " where ";
}
else
sel += " and ";
sel += "<your where condition 2>";
}
希望这就是你所问的问题。请注意这个粗略的例子。您可能想要使用StringBuilder
等
答案 1 :(得分:0)