我正在尝试修复搜索工具。这是我第一次遇到ASP.NET。当前搜索工具有一个单选按钮列表,其中包含三个如何搜索本地目录的选项。然而,在我之前从事这个项目的人没有完成代码并且已经退出。单选按钮不会影响搜索查询,因为我注意到无论您选择哪个选项,查询都是相同的。
这是我尝试重写搜索功能以合并三个单选按钮选项。但是,当我将此函数合并到代码的其余部分时,页面根本不会呈现,我没有收到错误。我不认为我在查询字符串中出错了,因为我采用了原始的错误并通过省略Contains语句来改变它。我假设错误来自我的if语句或我如何比较asp.net RadioButtonList ListItem值。
protected void btnclick_WorkspaceSearch(object sender, EventArgs e){
string strSearchTerm=tbSearch.Text.Trim()
if (rblSearchOption.SelectedValue == "all"){
// Find the search term in either a file name or file content
string indexQuery = "SELECT docauthor,doctitle, FileName, Path, Write, Size, Rank";
indexQuery += "FROM " + "Workspace" + "..SCOPE() WHERE ";
indexQuery += "CONTAINS(FileName, '\"" + strSearchTerm + "\"') ";
indexQuery += "OR CONTAINS(Contents, '\"" + strSearchTerm + "\"') ";
indexQuery += "ORDER BY Rank DESC";
}
if (rblSearchOption.SelectedValue=="names"){
// Find the search term in a file name
string indexQuery = "SELECT docauthor,doctitle, FileName, Path, Write, Size, Rank";
indexQuery += "FROM " + "Workspace" + "..SCOPE() WHERE ";
indexQuery += "CONTAINS(FileName, '\"" + strSearchTerm + "\"') ";
indexQuery += "ORDER BY Rank DESC";
}
if (rblSearchOption.SelectedValue =="contents") {
// Find the search term in a file's content
string indexQuery = "SELECT docauthor,doctitle, FileName, Path, Write, Size, Rank";
indexQuery += "FROM " + "Workspace" + "..SCOPE() WHERE ";
indexQuery += "CONTAINS(FileName, '\"" + strSearchTerm + "\"') ";
indexQuery += "ORDER BY Rank DESC";
}
searchIndex(indexQuery);
lit_strQueryString.Text = indexQuery;
}
答案 0 :(得分:0)
我弄明白了这个问题。对于那些评论感谢您的意见,我做了一些必要的更改,以帮助纠正潜在的错误。至于比较listItem值的原始问题,我使用的行是:
if (rblSearchOption.SelectedItem.Value =="contents"){
//logic here
}
我以前试过这个但是没用。我假设是因为评论中指出的错误。
附加说明(根据评论): 上面的代码缺失了;在第1行
字符串索引查询应该在if语句之外声明和启动。
再次感谢那些试图帮助我的人。
答案 1 :(得分:0)
很高兴你发现了你的问题,有点无关但看着你的代码我会说它需要一些严肃的重构。此代码可以通过以下方式简化:
1)使用switch语句。与代码不同,这将有助于评估单选按钮列表的选定值
2)使用StringBuilder构建查询
3)删除重复 - 名称和内容的附加逻辑完全相同,您可以通过使用两个案例表达式并提供一个语句来执行来删除重复。
System.Text.StringBuilder indexQuery = new System.Text.StringBuilder();
indexQuery.Append("SELECT docauthor,doctitle, FileName, Path, Write, Size, Rank FROM Workspace..SCOPE() WHERE ");
switch(rblSearchOption.SelectedItem.Value)
{
case "all":
indexQuery.AppendFormat("CONTAINS(FileName,'{0}') ",strSearchTerm);
indexQuery.AppendFormat("OR CONTAINS(Contents,'{0}')",strSearchTerm);
indexQuery.AppendLine("ORDER BY Rank DESC");
break;
case "names":
case "contents":
indexQuery.AppendFormat("CONTAINS(FileName,'{0}')",strSearchTerm);
indexQuery.Append("ORDER BY Rank DESC");
break;
}
searchIndex(indexQuery.ToString());
lit_strQueryString.Text = indexQuery;