您好,我有一个用 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/