3

嗨,我有一个使用 SQLDataSource 生成的 listView,Sql 从 URL 获取 2 个参数,然后执行 Select Query。

但我想测试参数 1st 的值,然后使用 If 更改 sql SelectCommand,否则如果

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

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


    }

知道我做错了什么吗?

4

1 回答 1

1

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

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

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

<asp:QueryStringParameter Name="client" QueryStringField="client" 
     DefaultValue="" ConvertEmptyStringToNull="True" />
于 2013-04-17T06:00:53.833 回答