Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在选择事件时向LinqDataSource添加Where参数_C#_Asp.net_Linq - Fatal编程技术网

C# 在选择事件时向LinqDataSource添加Where参数

C# 在选择事件时向LinqDataSource添加Where参数,c#,asp.net,linq,C#,Asp.net,Linq,我有一个使用LinqDataSource作为数据源的中继器。输入查询字符串时,我希望过滤结果,但仅当输入查询字符串时。如果没有查询字符串,则不应筛选结果,应返回所有结果 我试图在LinqDataSource的Selecting事件中向LinqDataSource添加Where参数,但它不起作用。这是我的密码: protected void ldsImages_Selecting(object sender, LinqDataSourceSelectEventArgs e) { if (R

我有一个使用LinqDataSource作为数据源的中继器。输入查询字符串时,我希望过滤结果,但仅当输入查询字符串时。如果没有查询字符串,则不应筛选结果,应返回所有结果

我试图在LinqDataSource的Selecting事件中向LinqDataSource添加Where参数,但它不起作用。这是我的密码:

protected void ldsImages_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    if (Request.QueryString["id"] != null)
    {
        e.WhereParameters.Add("ImageTypeID", Request.QueryString["id"]);
    }
}

我从未使用过LinqDataSource,但您是否可以不按所示查询结果? (代码摘自网站):

因此,您会有如下情况:

 protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        if(Request.QueryString["id"] != null)
        {
               var myImage = from img in imageSource
                             where img.ID == Request.QueryString["id"]
                             select img;
               e.Result = myImage;
        }
        else
        {
               e.Result = imageSource;
        }


    }

有几件事需要注意。首先,这是未测试的:D。其次,如果您的ID是整数,请不要忘记将querystring转换为整数。最后,您应该在使用查询字符串之前对其进行消毒。虽然这可能不是一个主要问题,但这是一个很好的做法。

我从未使用过LinqDataSource,但您能否按照所示查询结果? (代码摘自网站):

因此,您会有如下情况:

 protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        if(Request.QueryString["id"] != null)
        {
               var myImage = from img in imageSource
                             where img.ID == Request.QueryString["id"]
                             select img;
               e.Result = myImage;
        }
        else
        {
               e.Result = imageSource;
        }


    }

有几件事需要注意。首先,这是未测试的:D。其次,如果您的ID是整数,请不要忘记将querystring转换为整数。最后,您应该在使用查询字符串之前对其进行消毒。虽然这可能不是一个主要问题,但这是一个很好的做法。

您确定Request.QueryString[“id”]值不为null吗

是否在LinqDataSource上将AutoGenerateWhereClause属性设置为“true”


仔细阅读。是否确定Request.QueryString[“id”]值不为空

是否在LinqDataSource上将AutoGenerateWhereClause属性设置为“true”


仔细阅读。

添加一个onload事件来处理LinqDataSource_*\u Load()到LinqDataSource似乎可以让您执行以下操作:

protected void ldsAssets_Draft_Load(object sender, EventArgs e) {
    string Where_Statement = " Planner_ID == @Planner_ID";
    ldsAssets_Draft.WhereParameters.Add("Planner_ID", System.Data.DbType.Int32, User_ID.ToString());
    if (this._DraftOrderStatus != BusinessLogic.DraftOrderStatus.All) {
        Where_Statement += " AND Status_ID == @Status_ID";
        ldsAssets_Draft.WhereParameters.Add("Status_ID", System.Data.DbType.Int32, ((int)this._DraftOrderStatus).ToString());
    }
    ldsAssets_Draft.Where = Where_Statement;
}

因为它是在加载对象时调用的,而不是在选择时调用的——您仍然可以在没有太多代码的情况下使用您的对象。

向LinqDataSource添加一个onload事件来处理LinqDataSource\u*\ u load(),似乎可以让您执行以下操作:

protected void ldsAssets_Draft_Load(object sender, EventArgs e) {
    string Where_Statement = " Planner_ID == @Planner_ID";
    ldsAssets_Draft.WhereParameters.Add("Planner_ID", System.Data.DbType.Int32, User_ID.ToString());
    if (this._DraftOrderStatus != BusinessLogic.DraftOrderStatus.All) {
        Where_Statement += " AND Status_ID == @Status_ID";
        ldsAssets_Draft.WhereParameters.Add("Status_ID", System.Data.DbType.Int32, ((int)this._DraftOrderStatus).ToString());
    }
    ldsAssets_Draft.Where = Where_Statement;
}

由于它是在加载对象时调用的,而不是在选择时调用的,因此您仍然可以在没有太多代码的情况下使用对象。

LinqDataSource
的标记中,您仍然需要在
asp:LinqDataSource
标记的
Where
属性中声明参数

例如:

<asp:LinqDataSource ID="LDS_Images" runat="server" 
    ContextTypeName="DataContext" TableName="ImagesTable" 
    Where="ImageTypeID == @ImageTypeID ...>
 ...
</asp:LinqDataSource>

LinqDataSource
的标记中,仍然需要在
asp:LinqDataSource
标记的
Where
属性中声明参数

例如:

<asp:LinqDataSource ID="LDS_Images" runat="server" 
    ContextTypeName="DataContext" TableName="ImagesTable" 
    Where="ImageTypeID == @ImageTypeID ...>
 ...
</asp:LinqDataSource>

它怎么不起作用?返回错误的结果、运行时异常、未返回任何结果等@Cornelius-它不过滤结果,只返回所有结果。需要查看您的LinqDataSource声明才能诊断它是如何工作的?返回错误的结果、运行时异常、未返回任何结果等@Cornelius-它不过滤结果,只返回所有结果。需要查看LinqDataSource声明才能进行诊断