閲覧総計:&counter();  (本日:&counter(today);  昨日:&counter(yesterday);)

 GridViewのテキスト情報の検索キーワードを強調表示する手法は「[[検索キーワードの強調表示(多単語、多列)]]」
で確認できた。
 今回は、GridViewのHyperLink文字列の検索キーワードの背景をクリーム色にし、
本文中のどこに存在するかがひと目でわかる様に強調表示する方法を確認した。 09/10/25

【方法】
#ref(RowDataBound21.JPG);
※HyperLinkの列はCells(1)の「書籍名HL」
 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
     Dim SplitKeyWords As String() = TextBox1.Text.Split("  ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) ' 全角と半角のスペース
     If e.Row.RowType = DataControlRowType.DataRow Then
         For i As Integer = 0 To e.Row.Cells.Count - 1
             If i = 1 Then
                 Dim hyl As HyperLink = CType(e.Row.Cells(1).Controls(0), HyperLink)
                 For j As Integer = 0 To SplitKeyWords.Length - 1
                     hyl.Text = hyl.Text.Replace(SplitKeyWords(j), "<B Style='background-color:#fff495'>" & SplitKeyWords(j) & "</B>")
                 Next
             Else
                 For j As Integer = 0 To SplitKeyWords.Length - 1
                     e.Row.Cells(i).Text = e.Row.Cells(i).Text.Replace(SplitKeyWords(j), "<B Style='background-color:#fff495'>" & SplitKeyWords(j) & "</B>")
                 Next
             End If
         Next
     End If
 End Sub

開発環境:VWD2005 + Access2003
サーバ:ASP.NET2.0 + Access2003

【機能】GridViewのHyperLinkの「ASP.NET」の文字の背景がクリーム色になり強調
され、存在場所がひと目で把握できる。

【稼働画面】
#ref(GVHyperLinkEmphasize02.JPG);

Accessデータベース名:HonDB.mdb
テーブル名:HonTable
|~フィールド名|~データ型|~フィールドサイズ|
|ID (主キー)|オートナンバー型|長整数型|
|書籍名|テキスト型|100|
|出版社|テキスト型|50|
|ISBN10|テキスト型|20|

※VWD2005、ASP.NET2.0環境ではAccess2007のDBファイル形式(*.accdb)は利用できな
い模様。DBファイルは「Acces2002-2003形式(*.mdb)」で保存して利用する。


「キーワード検索」機能稼働ページの全コード
【GVHyperLinkEmphasize02.aspx】
 <%@ Page Language="VB" %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
 <script runat="server">
     
     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
         'スペースで区切られた単語のand検索を行う
         'TextBoxが空の場合は全件を表示
         AccessDataSource1.SelectParameters.Clear() 'パラメータをクリア 
         AccessDataSource1.SelectCommand = "SELECT [ID], [書籍名], [出版社], [ISBN10] FROM [HonTable]"
         Dim wa As String = " WHERE "
         Dim SplitKeyWords As String() = TextBox1.Text.Split("  ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) ' 全角と半角のスペース
         For i As Integer = 0 To SplitKeyWords.Length - 1
             AccessDataSource1.SelectParameters.Add("KeyWord" & i, "%" & SplitKeyWords(i) & "%")
             AccessDataSource1.SelectCommand &= wa & "(書籍名 LIKE @KeyWord" & i & " OR 出版社 LIKE @KeyWord" & i & ")"
             wa = " AND "
         Next
     End Sub
 
     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
         Dim SplitKeyWords As String() = TextBox1.Text.Split("  ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) ' 全角と半角のスペース
         If e.Row.RowType = DataControlRowType.DataRow Then
             For i As Integer = 0 To e.Row.Cells.Count - 1
                 If i = 1 Then
                     Dim hyl As HyperLink = CType(e.Row.Cells(1).Controls(0), HyperLink)
                     For j As Integer = 0 To SplitKeyWords.Length - 1
                         hyl.Text = hyl.Text.Replace(SplitKeyWords(j), "<B Style='background-color:#fff495'>" & SplitKeyWords(j) & "</B>")
                     Next
                 Else
                     For j As Integer = 0 To SplitKeyWords.Length - 1
                         e.Row.Cells(i).Text = e.Row.Cells(i).Text.Replace(SplitKeyWords(j), "<B Style='background-color:#fff495'>" & SplitKeyWords(j) & "</B>")
                     Next
                 End If
             Next
         End If
     End Sub
     
 </script>
 
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
     <title>GridViewのHyperLinkの特定単語の強調表示</title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
         GridViewのHyperLinkの検索キーワードの強調表示 09/10/25<br />
         <br />
         検索キーワード:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         <asp:Button ID="Button1" runat="server" Text="検索" OnClick="Button1_Click" /><br />
         <br />
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
             DataSourceID="AccessDataSource1" EmptyDataText="表示するデータ レコードがありません。" OnRowDataBound="GridView1_RowDataBound">
             <Columns>
                 <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
                 <asp:HyperLinkField DataNavigateUrlFields="ISBN10" DataNavigateUrlFormatString="http://bookweb.kinokuniya.co.jp/htm/{0}.html"
                     DataTextField="書籍名" DataTextFormatString="{0}" HeaderText="書籍名HL" />
                 <asp:BoundField DataField="出版社" HeaderText="出版社" SortExpression="出版社" />
             </Columns>
         </asp:GridView>
         <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="App_Data\honDB.mdb"
             DeleteCommand="DELETE FROM `HonTable` WHERE `ID` = ?" InsertCommand="INSERT INTO `HonTable` (`ID`, `書籍名`, `出版社`, `ISBN10`) VALUES (?, ?, ?, ?)"
             SelectCommand="SELECT `ID`, `書籍名`, `出版社`, `ISBN10` FROM `HonTable`" UpdateCommand="UPDATE `HonTable` SET `書籍名` = ?, `出版社` = ?, `ISBN10` = ? WHERE `ID` = ?">
             <DeleteParameters>
                 <asp:Parameter Name="ID" Type="Int32" />
             </DeleteParameters>
             <InsertParameters>
                 <asp:Parameter Name="ID" Type="Int32" />
                 <asp:Parameter Name="書籍名" Type="String" />
                 <asp:Parameter Name="出版社" Type="String" />
                 <asp:Parameter Name="ISBN10" Type="String" />
             </InsertParameters>
             <UpdateParameters>
                 <asp:Parameter Name="書籍名" Type="String" />
                 <asp:Parameter Name="出版社" Type="String" />
                 <asp:Parameter Name="ISBN10" Type="String" />
                 <asp:Parameter Name="ID" Type="Int32" />
             </UpdateParameters>
         </asp:AccessDataSource>
         <br />
     </div>
     </form>
 </body>
 </html>


【参考にしたページ】
「VS2005 ASP.NET GridView ハイパーリンク」 07/11/02
http://questionbox.jp.msn.com/qa3482817.html


#comment_nospam
#vote(参考になった[0],ふつう[0],参考にならなかった[0])

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS