2012-05-24 146 views
0

我設置了一個gridview來在網頁上顯示搜索結果。gridview不突出顯示搜索詞

我有下面的代碼是「應該」來代替搜索詞的任何實例,用粗體字的版本。

我試過很多不同的版本,但沒有任何工作。

Private Sub gvSearchResults_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSearchResults.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For Each cell As TableCell In e.Row.Cells 
      If cell.Text.Contains(searchTerm) Then 
       cell.Text = cell.Text.Replace(Session("SearchTerm"), "<span style='font-weight: bold;'>" & Session("SearchTerm") & "</span>") 
      End If 
     Next 
    End If 
End Sub 

在我的邏輯中是否有缺失?

謝謝!

+1

你運行一個調試,以驗證cell.Text不是空的?很多時候,gridviews會將這種性質的文本轉化爲字面控制。 –

+0

嗨,喬爾,謝謝......我剛剛做到了。出於某種原因,我所有的cell.text都是空白的...即使它們全都是網頁上的文字 – SkyeBoniwell

回答

1

如果Cell.Text爲空白,則可能是GridView控件放置Cell.Controls集合中文字控制。你應該把這樣的東西在:

If e.Row.RowType = DataControlRowType.DataRow Then 
    For Each cell As TableCell In e.Row.Cells 
     If cell.Controls.Count > 0 Then 
      Dim ltl as Literal = CType(cell.Controls(0), Literal) 
      If ltl.Text.Contains(searchTerm) Then 
       cell.Text = cell.Text.Replace(Session("SearchTerm"), "<span style='font-weight: bold;'>" & Session("SearchTerm") & "</span>") 
      End If 
     End If 
    Next 
End If 
+0

ltl.Text也是空的......但我看到結果在我的gridview在頁面上......當我看着Chrome開發工具中的單元格時,HTML看起來像這樣:​​心臟 SkyeBoniwell

+1

@ 999cm999:對,我見過那之前。這意味着集合中有多個控件。您應該嘗試1和2,以便了解它的進展情況。 –

+0

對不起... 1和2?我不確定你指的是什麼...... – SkyeBoniwell

1

代碼中有一個可疑的searchTerm變量。

觀察到,在你的代碼中使用的是searchTerm變量以及Session("SearchTerm")

我反而做:

searchTerm = Session("SearchTerm") 

If cell.Text.Contains(searchTerm) Then 
    cell.Text = cell.Text.Replace(searchTerm , "<span style='font-weight: bold;'>" & searchTerm & "</span>") 
End If 
+0

是的,我嘗試過...我把變量放在那裏希望能幫到 – SkyeBoniwell

+1

然後,你運行了一個調試Joel在評論中提出的建議?您是否驗證過cell.Text包含值?你的斷點是否使用了'cell.Text ='代碼? – Jeremy

+0

我放了一些斷點,它顯示爲cell.Text總是空的......這是沒有意義的,因爲我看到我的頁面上的所有結果 – SkyeBoniwell