简体   繁体   English

测试url参数的值然后从c#后面的代码更改SqlDataSource.SelectCommand

[英]Test the value of url parameters then Change SqlDataSource.SelectCommand from code behind c#

Hi I have a listView that I generated with a SQLDataSource, The Sql gets 2 parameters from the URL then performs a Select Query. 您好我有一个我用SQLDataSource生成的listView,Sql从URL获取2个参数然后执行选择查询。

But I want to test the value of the parameters 1st then change the sql SelectCommand using If, else If 但我想测试参数的值1st然后使用If更改sql SelectCommand,否则为

The Problem is my IF statements always fail and even when I remove them and change the query on page load, I always get returned the original data that I generated with the SQLDataSource even with the selectCommand deleted! 问题是我的IF语句总是失败,即使我删除它们并在页面加载时更改查询,我总是返回我使用SQLDataSource生成的原始数据,即使删除了selectCommand!

Here is part of my ASPX file 这是我的ASPX文件的一部分

        <asp:SqlDataSource ID="jobSearch" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="">
        <SelectParameters>
            <asp:QueryStringParameter Name="jobTitle" QueryStringField="jobTitle" Type="String" />
            <asp:QueryStringParameter Name="joblocation" QueryStringField="jobLocation" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Here is my .CS File 这是我的.CS文件

protected void Page_Load(object sender, EventArgs e)
    {
       // Request.QueryString["jobTitle"]

        string jobTitle = Request.QueryString["jobTitle"];
        string jobLocation = Request.QueryString["jobLocation"];

        if (!string.IsNullOrEmpty(jobTitle) && !string.IsNullOrEmpty(jobLocation))
        {
            jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] WHERE FREETEXT (([jobTitle]), @jobTitle) AND FREETEXT (([location]), @location) ORDER BY [jobCreated]";
            test.Text = "1st if " + jobTitle;
        }
        else if (jobTitle == string.Empty && !string.IsNullOrEmpty(jobLocation) || string.IsNullOrWhiteSpace(jobTitle) && !string.IsNullOrEmpty(jobLocation) || string.IsNullOrEmpty(jobTitle) && !string.IsNullOrEmpty(jobLocation) || jobTitle == null && !string.IsNullOrEmpty(jobLocation))
        {

             jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] WHERE FREETEXT (([location]), @location) ORDER BY [jobCreated]";

             test.Text = "1st else if " + jobTitle;
        }

        else if (string.IsNullOrEmpty(jobTitle) && string.IsNullOrEmpty(jobLocation))
        {
            jobSearch.SelectCommand = "SELECT [jobId], [userId], [jobTitle], [jobBody], [jobPosition], [JobType], [salaryFrom], [salaryTo], [salaryType], [location], [jobCreated], [jobEnd], [jobViews], [applications] FROM [recruiter_Jobs] ORDER BY [jobCreated]";

            test.Text = "last else if " + jobTitle;
        }


    }

Any idea what I am doing wrong? 知道我做错了什么吗?

SqlDataSource won't fire if any of it's parameters are null, unless you specify otherwise: 如果SqlDataSource的任何参数为null,则不会触发它,除非您另行指定:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

It might also be necessary to add a null default value to your querystring parameter: 可能还需要在querystring参数中添加一个null默认值:

<asp:QueryStringParameter Name="client" QueryStringField="client" 
     DefaultValue="" ConvertEmptyStringToNull="True" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在后面的代码中使用 SqlDataSource.SelectCommand 中的参数化查询? - How to use parameterized queries in SqlDataSource.SelectCommand in code behind? 使用带参数的存储过程修改SqlDataSource.SelectCommand - Modify SqlDataSource.SelectCommand using Stored Procedure with parameters 使用C#中的代码编写SQLdatasource以使用值后面的代码 - Writing a SQLdatasource in code behind in C# to use a code behind value 如果文本框为空,则不会通过更改SqlDataSource.SelectCommand来填充ASP.NET GridView - ASP.NET GridView won't populate from changing SqlDataSource.SelectCommand if Textbox is empty 需要从C#修改SqlDataSource的SelectCommand参数 - Need to modify the SelectCommand parameter for SqlDataSource from C# 在C#.net后面的代码中访问我的SqlDataSource中的数据 - Accessing data from my SqlDataSource in the code behind C# .net 在代码后面创建一个SelectCommand,与在SQLDataSource中创建一个相同 - Creating a SelectCommand in code behind which works identically to creating one in the SQLDataSource 从后面的代码更改SqlDataSource中的选择查询 - Change select query in SqlDataSource from code behind 将C#SqlCommand链接到aspx页面上的SqlDataSource的SelectCommand - Link C# SqlCommand to the SelectCommand of SqlDataSource on aspx page c#从后面的代码更改javascript变量的值 - c# change value of javascript variable from code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM