不能从querystring关键字正确显示搜索结果

时间:2010-10-15 18:05:02

标签: asp.net

我有一个basicSearch页面与关键字搜索完美配合(在搜索结果页面上突出显示关键字)但是当我修改它以使用查询字符串时,它会显示所有突出显示关键字的记录。

aspx页面:

<form id="form1" runat="server">

<div id="mainContent">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <script type="text/javascript">
        function updated() {
            //  close the popup
            tb_remove();

            //  refresh the update panel so we can view the changes  
            $('#<%= me.btnRefreshResources.ClientID %>').click();
        }

        function pageLoad(sender, args) {
            if (args.get_isPartialLoad()) {
                //  reapply the thick box stuff
                tb_init('a.thickbox');
            }
        }
    </script>  

    <asp:AccessDataSource ID="AccessDataSource" runat="server" 
        DataFile="~/Dir/search.mdb" 
        SelectCommand="SELECT * FROM [searches] ORDER BY [Title]"  
        FilterExpression="Title like '%{0}%' or LastName like '%{1}%' or FirstName like '%{2}%' or Description like '%{3}%' ">

        <FilterParameters>
            <asp:ControlParameter Name="Title" ControlID="txtSearch" PropertyName="Text" />
            <asp:ControlParameter Name="LastName" ControlID="txtSearch" PropertyName="Text" />
            <asp:ControlParameter Name="FirstName" ControlID="txtSearch" PropertyName="Text" />
            <asp:ControlParameter Name="Description" ControlID="txtSearch" PropertyName="Text" />
        </FilterParameters>
    </asp:AccessDataSource>

    <div id="basicSearch" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <b style="font-family: Arial, Helvetica, sans-serif; font-size: 18px">Enter a keyword: </b><asp:TextBox ID="txtSearch" runat="server" Width="300px" Font-Size="18px" />&nbsp;&nbsp;
        <asp:Button ID="btnBasicSearch" Text="Search" Runat="Server"/>&nbsp;&nbsp;
        <asp:Button ID="btnBasicClear" Text="Clear" Runat="Server"/><br /><br />
    </div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>      
        <asp:Button ID="btnRefreshResources" runat="server" style="display:none" OnClick="Refresh_Click" /> 

    <asp:GridView ID="gvResources" runat="server" DataSourceID="AccessDataSource" CssClass="datagrid" GridLines="None" AutoGenerateColumns="false" AllowSorting="True" PageSize="50" AllowPaging="True" Width="100%"  OnPageIndexChanging="gvResources_PageIndexChanging"> <PagerSettings Position="TopAndBottom" />
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" />

    <asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Right" SortExpression="Title">
        <ItemTemplate>
            <%# DisplayType(Eval("Book"))%>
        </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Title" SortExpression="Title">
    <ItemTemplate>
        <asp:Label ID="lblTitle" Text='<%# HighlightText(Eval("Title").ToString()) %>' runat="server" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
    <ItemTemplate>
        <asp:Label ID="lblLastName" Text='<%# HighlightText(Eval("LastName").ToString()) %>' runat="server" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
    <ItemTemplate>
        <asp:Label ID="lblFirstName" Text='<%# HighlightText(Eval("FirstName").ToString()) %>' runat="server" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
        <asp:Label ID="lblDescription" Text='<%# HighlightText(Eval("Description").ToString()) %>' runat="server" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField>
        <ItemTemplate>
            <a id="btnShowPopup" runat="server" class="thickbox" title='<%# Eval("ID", "Request Resource ID: {0}") %>' href='<%# Eval("ID", "RequestResource.aspx?ID={0}&TB_iframe=true&height=350&width=500&modal=true") %>'>Request This Item</a> </ItemTemplate>
    </asp:TemplateField>

    </Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel> 
</div>
</form>

代码背后:

Partial Class BasicSearch

Inherits System.Web.UI.Page

Dim SearchString As String = ""

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    SearchString = Request.QueryString("Keyword")
End Sub

Protected Sub Refresh_Click(ByVal sender As Object, ByVal args As EventArgs)
    ' update the grids contents
    Me.gvResources.DataBind()
End Sub

Function HighlightText(ByVal InputTxt As String) As String
    If SearchString = "" Then
        Return InputTxt
    Else
        Dim ResultStr As Regex
        ResultStr = New Regex(SearchString.Replace(" ", "|"), RegexOptions.IgnoreCase)
        Return ResultStr.Replace(InputTxt, New MatchEvaluator(AddressOf ReplaceWords))
    End If
End Function

Public Function ReplaceWords(ByVal m As Match) As String
    Return "<span class=highlight>" + m.ToString + "</span>"
End Function

Protected Sub gvResources_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    gvResources.PageIndex = e.NewPageIndex
    SearchString = txtSearch.Text
    gvResources.DataBind()
End Sub

Protected Sub btnBasicSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBasicSearch.Click
    SearchString = txtSearch.Text
    Response.Redirect("BasicSearch.aspx?Keyword=" & SearchString)
End Sub

Protected Sub btnBasicClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBasicClear.Click
    txtSearch.Text = ""
    SearchString = ""
End Sub

Protected Function DisplayType(ByVal Book As Boolean) As String
    If Book Then
        Return "Book"
    Else
        Return "Other"
    End If
End Function
End Class

1 个答案:

答案 0 :(得分:0)

只是猜测 - SearchString函数执行时HighlightText的值是多少?您将查询字符串存储在本地页面变量中,该变量仅在页面请求执行时存在。

如果在未调用HighlightText的情况下调用Page_Load(在同一请求中),则SearchString将为空。