当属性为 null 时,在 FilterParameters 中使用 ControlParameter

发布于 2024-08-29 09:23:16 字数 259 浏览 4 评论 0原文

我有一个 DataList 和 FormView;他们有不同的数据源,尽管他们提取相同的信息。 FormView 的数据源有一个 FilterExpression 来提取 DataList 上选择的任何内容。第一次加载时,DataList 的 SelectedValue 为 null(自然)。我希望 FilterExpression 产生零行,但事实并非如此。如果我将 DefaultValue 设置为 0,它会执行此操作,但当我从 DataList 中选择某些内容时,该参数永远不会更新。我做错了吗?

I have a DataList and FormView; they have separate datasources, though they pull the same info. The FormView's datasource has a FilterExpression to pull whatever's been selected on the DataList. On first load, the SelectedValue of the DataList is null (naturally). I expect the FilterExpression to result in zero rows, but it doesn't. If I set the DefaultValue to 0, it does, but then the parameter never updates when I select something from the DataList. Am I doing it wrong?

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

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

发布评论

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

评论(1

秋千易 2024-09-05 09:23:16

事实证明,(根据这篇文章) 这有一个错误。解决方案是附加一个像这样的 OnFiltering 处理程序(我做了一些增强):

protected void FilteringCheck(Object sender, SqlDataSourceFilteringEventArgs e)
{
    // Make sure there are no null parameters.
    for (int i = 0; i < e.ParameterValues.Count; i++)
    {
        if (e.ParameterValues[i] == null)
        {
            switch (((System.Web.UI.WebControls.SqlDataSourceView)sender).FilterParameters[i].Type)
            {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    e.ParameterValues[i] = -1;
                    break;
                case TypeCode.Byte:
                    e.ParameterValues[i] = 0;
                    break;
                case TypeCode.Char:
                case TypeCode.String:
                    e.ParameterValues[i] = string.Empty;
                    break;
                case TypeCode.DateTime:
                    e.ParameterValues[i] = new DateTime();
                    break;
            }
        }
    }
}

Turns out, (according to this post) there's a bug with this. The solution is to attach an OnFiltering handler like this (I've made a few enhancements):

protected void FilteringCheck(Object sender, SqlDataSourceFilteringEventArgs e)
{
    // Make sure there are no null parameters.
    for (int i = 0; i < e.ParameterValues.Count; i++)
    {
        if (e.ParameterValues[i] == null)
        {
            switch (((System.Web.UI.WebControls.SqlDataSourceView)sender).FilterParameters[i].Type)
            {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    e.ParameterValues[i] = -1;
                    break;
                case TypeCode.Byte:
                    e.ParameterValues[i] = 0;
                    break;
                case TypeCode.Char:
                case TypeCode.String:
                    e.ParameterValues[i] = string.Empty;
                    break;
                case TypeCode.DateTime:
                    e.ParameterValues[i] = new DateTime();
                    break;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文