Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp:hyperlink文本中以粗体显示某些选定单词_C#_Asp.net_Hyperlink - Fatal编程技术网

C# 如何在asp:hyperlink文本中以粗体显示某些选定单词

C# 如何在asp:hyperlink文本中以粗体显示某些选定单词,c#,asp.net,hyperlink,C#,Asp.net,Hyperlink,我在.aspx上有一个asp:repeater控件,在代码背后,我将其数据源绑定到类型为KeyValuePair[]的集合。我选择了literal,这样我就可以用或html标记在literal文本中围绕选定的单词。我成功地做到了这一点,但我没有找到在asp:repeater的asp:hyperlink文本部分显示文本的方法 我的.aspx代码如下所示: <asp:Repeater ID="repLinks" runat="server"> <ItemTemplat

我在.aspx上有一个asp:repeater控件,在代码背后,我将其数据源绑定到类型为
KeyValuePair[]
的集合。我选择了literal,这样我就可以用
html标记在literal文本中围绕选定的单词。我成功地做到了这一点,但我没有找到在
asp:repeater
asp:hyperlink
文本部分显示文本的方法

我的.aspx代码如下所示:

<asp:Repeater ID="repLinks" runat="server">
       <ItemTemplate>
           <div onclick="window.open('<%# ((KeyValuePair<Literal,string>)Container.DataItem).Value %>','_blank');">
               <div>
                   <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl="<%# ((KeyValuePair<Literal,string>)Container.DataItem).Value %>" Text="<%#((KeyValuePair<Literal,string>)Container.DataItem).Key.Text %>"
                       Font-Size='Large' ForeColor='Blue' Font-Names="Open Sans" CssClass="linkstyle" />
                   <br />
               </div>
           </div>
       </ItemTemplate>
   </asp:Repeater>
char[] seperator = { ' ' };
String[] explodedString = Results1[index].Key.Split(seperator);
List<String> Query= new List<string>(TextBox1.Text.Trim().ToLowerInvariant().Split(seperator,StringSplitOptions.RemoveEmptyEntries));
for (int i = 0; i < explodedString.Length; i++)
    {
       if (Query.Contains(explodedString[i].ToLowerInvariant()) == true)
          {
               explodedString[i] = "<strong>" + explodedString[i] + "<strong>";
          }
    }
 Literal temp = new Literal();
 temp.Text = explodedString.ToString();
 TryCurrentWindow[index] = new KeyValuePair<Literal, string>(temp, Results1[index].Value);

这里的TryCurrentWindow是KeyValuePair[],而
explodedstring[]
是由我要修改的
''字符分割的文本字符串,查询[]是我的关键字列表

问题

  • 您不能像这样关闭标签

  • 您只需执行
    ToString()
    string[]
    数组

  • 更新此行:

     explodedString[i] = "<strong>" + explodedString[i] + "</strong>";
    
    使用此

    Literal temp = new Literal();
    temp.Text =  string.Join(" ", explodedString); 
    
    char[] seperator = { ' ' };
    String[] explodedString = Results1[index].Key.Split(seperator);
    List<String> Query= new List<string>(TextBox1.Text.Trim().ToLowerInvariant().Split(seperator,StringSplitOptions.RemoveEmptyEntries));
    
    for (int i = 0; i < explodedString.Length; i++)
    {
        if (Query.Contains(explodedString[i].ToLowerInvariant()) == true)
        {
           explodedString[i] = "<strong>" + explodedString[i] + "</strong>"; //changed
        }
    }
    
    Literal temp = new Literal();
    temp.Text = string.Join(" ", explodedString); //changed 
    TryCurrentWindow[index] = new KeyValuePair<Literal, string>(temp, Results1[index].Value);
    
    As索引在
    字符串[]
    数组中修改。您需要加入数组才能修改
    string[]
    array

    更新的代码片段

    Literal temp = new Literal();
    temp.Text =  string.Join(" ", explodedString); 
    
    char[] seperator = { ' ' };
    String[] explodedString = Results1[index].Key.Split(seperator);
    List<String> Query= new List<string>(TextBox1.Text.Trim().ToLowerInvariant().Split(seperator,StringSplitOptions.RemoveEmptyEntries));
    
    for (int i = 0; i < explodedString.Length; i++)
    {
        if (Query.Contains(explodedString[i].ToLowerInvariant()) == true)
        {
           explodedString[i] = "<strong>" + explodedString[i] + "</strong>"; //changed
        }
    }
    
    Literal temp = new Literal();
    temp.Text = string.Join(" ", explodedString); //changed 
    TryCurrentWindow[index] = new KeyValuePair<Literal, string>(temp, Results1[index].Value);
    
    char[]分隔符={''};
    String[]explodestring=Results1[index].Key.Split(分隔符);
    列表查询=新列表(TextBox1.Text.Trim().ToLowerInvariant().Split(分隔符,StringSplitOptions.RemoveEmptyEntries));
    for(int i=0;i”+explodestring[i]+“”;//已更改
    }
    }
    文字温度=新文字();
    temp.Text=string.Join(“,explodestring”)//改变
    TryCurrentWindow[index]=新的键值对(temp,Results1[index].Value);
    
    您能举例说明您对给定输入关键字文本的输出期望值吗?您也能发布代码,说明如何创建
    KeyValuePair[]
    集合吗?假设关键字是“united”和“alliance”,而“united progressive alliance”中的文本我需要输出为“united progressive alliance”,因为代码太长,无法在此处发布,我会试着简单地解释,一旦我收到我的文本,我将其拆分为一个字符串数组,并将每个字符串与我的关键字进行比较,如果它们匹配,我将它们用
    标记包围,并将修改后的文本存储到
    asp:Literal
    文本中,然后将其添加到keyValuePair中,其中关键部分是我感兴趣的文本,值部分是一些描述字符串。您可以编辑在那里发布并添加代码。