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

标签 c# asp.net if-statement sqldatasource url-parameters

您好,我有一个用 SQLDataSource 生成的 listView,Sql 从 URL 获取 2 个参数,然后执行选择查询。

但我想先测试参数的值,然后使用 If 或 If 更改 sql SelectCommand

问题是我的 IF 语句总是失败,即使我删除它们并在页面加载时更改查询,我总是会返回我使用 SQLDataSource 生成的原始数据,即使删除了 selectCommand!

这是我的 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>

这是我的 .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;
        }


    }

知道我做错了什么吗?

最佳答案

如果它的任何参数为空,SqlDataSource 将不会触发,除非您另外指定:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

可能还需要为您的查询字符串参数添加一个空默认值:

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

关于c# - 测试 url 参数的值,然后从 c# 后面的代码更改 SqlDataSource.SelectCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16052553/

相关文章:

javascript - JS if else if else

excel - 获取多个规模不同的组的组平均值

c# - TSQL 从递归表中获取所有行

c# - .NET Web 服务返回无效的 XML

c# - 将 JSON 绑定(bind)到模型时使 WebAPI 拒绝未知字段

asp.net - Ext.net:如何设置百分比而不是像素的宽度?

javascript - onSuccess 和 onFailure 不会被解雇

mysql - Vb.Net/MySql 查询失败 - 未产生预期结果

c# - 从连接的 TcpClient 获取 UdpClient?

c# - 在 C# 中,是一个声明为 : int[]a = {numbers . ..} 的数组;在堆栈中?