How to highlight or change the color of some words in a shortcut dynamically at runtime?

I have a label containing some text, and I want to highlight or change the color of some words in the label text, not all words. It must be dynamic. Any suggestions?

This is for C # with ASP.NET in a user control in a web part in sharepoint

+5
source share
7 answers

On the server side, you can simply insert some HTML in the label text (VB):

myLabel.Text="Some normal text <span style='color: red;'>some red text</span>"

That the basic mechanism, but "dynamic", can mean a lot of things here. If you post more details on what you are doing, I could help more.

: , Literal , Html, Label , .

: fooobar.com/questions/315515/...

, , , .

+10

ASP.NET,

, <span>. <span> background-color CSS .

,

<asp:Label runat="server">
    <span style="background-color:Blue;">Hello</span> World
</asp:Label>

<asp:Label runat="server" Text="<span style='background-color:Blue;'>Hello</span> World" />

EDIT:

, -

 StringBuilder builder = new StringBuilder();
 builder.Append([start of text]);
 builder.Append("<span style=\"background-color:Blue;\">");
 builder.Append([text to highlight]);
 builder.Append("</span>");
 builder.Append([rest of text]);

 Label.Text = builder.ToString();

, -

 string theTextToMatch = "[Text to match]";
 string theText = Label.Text;

 int leftIndex = theText.IndexOf(theTextToMatch, StringComparison.OrdinalIgnoreCase);
 int rightIndex = leftIndex + theTextToMatch.Trim().Length;

 StringBuilder builder = new StringBuilder();
 builder.Append(theText , 0, leftIndex);
 builder.Append("<span style=\"background-color:Blue;\">");
 builder.Append(theText, leftIndex, rightIndex - leftIndex);
 builder.Append("</span>");
 builder.Append(theText, rightIndex, theText.Length - rightIndex);

 Label.Text = builder.ToString();
+6

. ? - ASP.NET #? Windows? .

+1

:

<label> She sells sea shells by the sea shore </label>

, " " , " " .

<label> She <font color="red">sea shells</font> by <font style="BACKGROUND-COLOR: yellow">the sea shore</font></label>

!

+1

, .

Function Remarcar(ByVal palabra As String, ByVal texto As String) As String

    Dim textoNuevo As String = String.Empty

    If Not String.IsNullOrEmpty(palabra) Then
        Dim split As String() = texto.Split(New Char() {" "c})

        For Each str As String In split
            If str.ToLower.Contains(palabra.ToLower) Then

                Dim a As String = String.Empty
                Dim b As Int32

                For i = 0 To str.Length
                    If str.ToLower.Substring(i, palabra.Length) = palabra.ToLower Then
                        a = str.Substring(i, palabra.Length)
                        b = i
                        Exit For
                    End If
                Next

                textoNuevo &= str & " "

                textoNuevo = textoNuevo.Replace(str.Substring(b, palabra.Length), "<span style=""background-color:Yellow;"">" & a & "</span>")
            Else
                textoNuevo &= str & " "
            End If
        Next
    Else
        textoNuevo = texto
    End If

    Return textoNuevo
End Function





        Dim texto As String = "I made a function to look up words in a text string and highlight them with color, the result is put into a label."

        Label1.Text = Remarcar("highlight", texto)
+1

asp.net( ), , , .

    <asp:label runat="server" id="nonRed">some text 
        <asp:label runat="server" id="redText" style="color:Red">Red Text</asp:label>
   </asp:label>
0

You can use the substitution control if there is a problem with caching.

<asp:Label ID="Label1" runat="server" Text="">
    <asp:Substitution ID="Substitution1" runat="server" MethodName="GetDynamicLabel"/>
</asp:Label>

protected static string GetDynamicLabel( HttpContext context )
{
    return string.Format( "<span style='background-color:Blue;'>{0}</span> {1}", "Blue", "Not Blue" );
}
0
source

All Articles