2013-04-17 29 views
3

嗨我有一个listView,我用SQLDataSource生成,Sql从URL获取2个参数,然后执行Select Query。测试url参数的值,然后从代码后面更改SqlDataSource.SelectCommand#

但我要测试的参数,第一个,然后更改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; 
     } 


    } 

任何想法,我做错了什么?

回答

1

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

<asp:SqlDataSource CancelSelectOnNullParameter="False" /> 

它也需要一个空默认值添加到您的查询参数:

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

刚刚尝试过,但我得到一个空或全空文本谓词错误 –

+0

这确实工作,这是我的SQL是错的:-)谢谢 –