测试url参数的值,然后更改SqlDataSource.从c#后面的代码中选择命令

本文关键字:代码 命令 选择 参数 url 然后 测试 SqlDataSource | 更新日期: 2023-09-27 18:09:30

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

但是我想先测试参数的值,然后使用If改变sql SelectCommand,否则If

问题是我的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;
        }

    }

你知道我做错了什么吗?

测试url参数的值,然后更改SqlDataSource.从c#后面的代码中选择命令

如果SqlDataSource的任何参数为null,则不会触发,除非您另行指定:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

可能还需要在querystring参数中添加一个空默认值:

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