如何比较ASP.NET单选按钮列表的值

3
我正在尝试修复一项工作中的搜索工具。这是我第一次遇到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;
}

4
可能这不是主要问题,但在您的可选字段列表中,单词“Rank”后面需要有一个空格。在“Rank”和“FROM”之间需要有一个空格。 - DOK
1
当你右键单击 searchIndex(indexQuery); 并说“导航到定义”时,indexQuery 在哪里定义?这三个 indexQuery 变量在 if 语句中具有局部作用域,因此无法在外部作用域(整个方法)中使用。此外,第一行末尾缺少一个 ;。你是从实际的工作代码库中复制/粘贴的吗? - mellamokb
这也不是主要问题,但你的第三个选项返回与第二个选项相同的indexQuery。你不想让它说“CONTAINS(Contents,......”吗? - Melanie
当您通过调试器运行此代码时,在 searchIndex(indexQuery); 这一行中 indexQuery 的值是多少?searchIndex() 函数是否返回一个值?查询结果会发布到哪个页面? - Melanie
我无法访问lit_strQueryString,但searchIndex()使用查询字符串调用数据库查询。它从数据库获取结果表并将其移动到Dataview,然后将Dataview绑定到datagrid。 - Ishan Patel
显示剩余6条评论
2个回答

0

我找到了问题所在。对于那些评论的人,谢谢你们的意见,我进行了一些必要的更改以帮助纠正潜在的错误。至于最初的问题,比较listItem值的代码行是:

if (rblSearchOption.SelectedItem.Value =="contents"){
//logic here
}

我之前尝试过这个,但是没有成功。我猜测是因为评论中指出的错误。

根据评论的补充说明: 第一行代码缺少一个分号; 字符串索引查询应该在if语句之外声明和初始化。

再次感谢那些试图帮助我的人。


0
很高兴你找到了问题,虽然有点不相关,但是看着你的代码,我觉得它需要进行一些严重的重构。这段代码可以通过以下方式简化:
1)使用switch语句。这将有助于仅评估单选按钮列表的选择值,而不像你的代码那样多次评估。
2)使用StringBuilder来构建查询。
3)消除重复 - 对于namescontents的附加逻辑完全相同,您可以使用两个case表达式并提供一个要执行的语句来消除重复。
  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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接