ObjectDataSource FilterExpression 不适用于多个参数

发布于 2024-12-25 22:40:04 字数 1418 浏览 2 评论 0原文

我有一个可以很好地绑定到我的 GridView 的 ObjectDataSource。

我还有两个调用 ApplyFilterExpression 函数的下拉列表框。

它们都是独立工作的,但是当我设置两个下拉列表的值时,我没有得到任何结果。但我知道应该有结果。我在这里做错了什么?

<asp:ObjectDataSource ID="dsLogs" runat="server" SelectMethod="GetDTAll" 
    TypeName="LogManager.LogRepository" 
    SelectCountMethod="GetAllCount" >
    <SelectParameters>
        <asp:Parameter DefaultValue="DESC" Name="sortOrder" Type="String" />
        <asp:Parameter DefaultValue="timestamp" Name="orderBy" Type="String" />
        <asp:Parameter DefaultValue="1" Name="startRowIndex" Type="Int32" />
        <asp:Parameter DefaultValue="1000" Name="maximumRows" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

private void ApplyFilterExpression()
{
    dsLogs.FilterExpression = null;
    bool hasFilter = false;

    if (ddlTypes.SelectedIndex != 0)
    {
        dsLogs.FilterExpression = "type='" + ddlTypes.SelectedValue + "'";
        hasFilter = true;
    }

    if (ddlUsers.SelectedIndex != 0)
    {
        if (hasFilter)
        {
            dsLogs.FilterExpression += " AND username='" + ddlUsers.SelectedValue + "'";
        }
        else
        {
            dsLogs.FilterExpression = "username='" + ddlUsers.SelectedValue + "'";
            hasFilter = true;
        }
    }
    ViewState["FiltExp"] = (string)dsLogs.FilterExpression;
}

I have an ObjectDataSource that is binding to my GridView just fine.

I also have two dropdown list boxes that call an ApplyFilterExpression function.

They both work independently, but when i set both dropdown with values i get no results. But i KNOW that there should be results. What am i doing wrong here?

<asp:ObjectDataSource ID="dsLogs" runat="server" SelectMethod="GetDTAll" 
    TypeName="LogManager.LogRepository" 
    SelectCountMethod="GetAllCount" >
    <SelectParameters>
        <asp:Parameter DefaultValue="DESC" Name="sortOrder" Type="String" />
        <asp:Parameter DefaultValue="timestamp" Name="orderBy" Type="String" />
        <asp:Parameter DefaultValue="1" Name="startRowIndex" Type="Int32" />
        <asp:Parameter DefaultValue="1000" Name="maximumRows" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

private void ApplyFilterExpression()
{
    dsLogs.FilterExpression = null;
    bool hasFilter = false;

    if (ddlTypes.SelectedIndex != 0)
    {
        dsLogs.FilterExpression = "type='" + ddlTypes.SelectedValue + "'";
        hasFilter = true;
    }

    if (ddlUsers.SelectedIndex != 0)
    {
        if (hasFilter)
        {
            dsLogs.FilterExpression += " AND username='" + ddlUsers.SelectedValue + "'";
        }
        else
        {
            dsLogs.FilterExpression = "username='" + ddlUsers.SelectedValue + "'";
            hasFilter = true;
        }
    }
    ViewState["FiltExp"] = (string)dsLogs.FilterExpression;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2025-01-01 22:40:04

它可能与更新过滤器表达式有关,但我怀疑情况确实如此。

但是,您可以尝试以下代码,看看它是否可以解决您的问题(它将节省您一些代码并使其更易于理解和维护):

private void ApplyFilterExpression()
{
    var sbFilter = new System.Text.StringBuilder(200);

    if (ddlTypes.SelectedIndex != 0)
    {
        sbFilter.Append("type='").Append(ddlTypes.SelectedValue).Append("'");
    }

    if (ddlUsers.SelectedIndex != 0)
    {
        if (sbFilter.Length != 0)
        {
            sbFilter.Append(" AND ");
        }
        sbFilter.Append("username='").Append(ddlUsers.SelectedValue).Append("'");
    }
    if (sbFilter.Length != 0)
    {
        dsLogs.FilterExpression = sbFilter.ToString();
    } else
    {
        dsLogs.FilterExpression = null;
    }
}

It could have something to do with updating filter expression, but I doubt that is the case.

However, you could try the following code to see if it resolves your problem (it will save you some code and make it easier to understand and maintain):

private void ApplyFilterExpression()
{
    var sbFilter = new System.Text.StringBuilder(200);

    if (ddlTypes.SelectedIndex != 0)
    {
        sbFilter.Append("type='").Append(ddlTypes.SelectedValue).Append("'");
    }

    if (ddlUsers.SelectedIndex != 0)
    {
        if (sbFilter.Length != 0)
        {
            sbFilter.Append(" AND ");
        }
        sbFilter.Append("username='").Append(ddlUsers.SelectedValue).Append("'");
    }
    if (sbFilter.Length != 0)
    {
        dsLogs.FilterExpression = sbFilter.ToString();
    } else
    {
        dsLogs.FilterExpression = null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文